Rails 数据库关联/迁移
我正在学习 Rails,并试图找出数据库关联。如果我有一个数据库表,其中包含具有 ID、姓名、电子邮件等的用户,以及包含消息、发件人(用户)和收件人(也是用户)的消息表,我如何设置迁移和模型。在这里,我使用的是 Rails 3.1。我很确定我可以只使用消息表中的一个用户和引用:迁移中的用户来完成此操作,但我不确定如何设置其中两个。
I'm learning rails and I'm trying to figure out database associations. If I have a database table with say Users that has an id, name, email, etc. and a Message table with the message, a sender (User) and recipient (also a User), how do I set up the migration and the models. Here, I'm using Rails 3.1. I'm pretty sure I can do it with just one User in the Message table with the references:User in the migration, but I'm not sure how to set up two of them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些帮助您入门的资源:Rails 教程:用户模型< /a> RoR 指南:迁移
首先进行用户迁移
,然后迁移消息,
然后在您的消息模型 (/app/models/messages.rb)
和用户模型 (/app/models/users.rb)
显然,这是需要发生的事情的粗略草图,但这应该可以帮助您开始!
Some resources to get you started: Rails Tutorial: User Model RoR Guides: Migrations
First make your user migration
Then your messages migration
Then in your Messages model (/app/models/messages.rb)
And in your User model (/app/models/users.rb)
Obviously this is a rough sketch of what needs to happen, but that should get you started!
好的,这就是我的最终结果。
首先是迁移...
接下来是消息模型...
以及用户模型...
OK, here's what I ended up with.
First the migration ...
Next in the Message model ...
and in the User model ...