参考模型编写两次迁移
我有一个消息模型(Message),该模型作为 userTo 和 userFrom,因此有两个对 User 的引用。我该如何编写迁移?我的用户模型是User。
谢谢
I have a message model (Message) and this models as a userTo and userFrom, so two references to User. How can i write the migration? My user model is User.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是这个问题的完整答案,以防访问这个问题的人很难将所有内容放在一起(就像我第一次研究这个问题时一样)。
答案的某些部分发生在您的迁移中,某些部分发生在您的模型中:
迁移
这里您指定此表中有两列将被称为 :sender 和 :recipient 并保存对另一个表的引用。 Rails 实际上会为您创建名为“sender_id”和“recipient_id”的列。在我们的例子中,它们将各自引用 Users 表中的行,但我们在模型中指定,而不是在迁移中。
模型
这里您将在名为 :sender 的 Message 模型上创建一个属性,然后指定该属性将引用 User 类的实例。 Rails 看到“belongs_to”后,会在数据库中查找名为“sender_id”(我们在上面定义)的列,并使用它来存储外键。然后你就为收件人做同样的事情。
这将允许您通过消息模型的实例访问发送者和接收者(用户模型的两个实例),如下所示:
这是您的用户模型:
在这里,您在用户模型上创建一个名为 :sent_messages 的属性,指定该属性与消息模型相关,并且消息模型上与该属性相关的外键称为“sender_id”。然后你对收到的消息做同样的事情。
这允许您通过执行以下操作来获取所有用户发送或接收的消息:
执行其中任一操作将返回消息模型实例的数组。
Here's a complete answer to this issue, in case people visiting this question are having a hard time putting everything together (as I was when I first looked into this).
Some parts of the answer take place in your Migrations and some in your Models:
Migrations
Here you are specifying that there are two columns in this table that will be referred to as :sender and :recipient and which hold references to another table. Rails will actually create columns called 'sender_id' and 'recipient_id' for you. In our case they will each reference rows in the Users table, but we specify that in the models, not in the migrations.
Models
Here you are creating a property on the Message model named :sender, then specifying that this property will be referencing an instance of the User class. Rails, seeing the "belongs_to", will look for a column in your database called "sender_id", which we defined above, and use that to store the foreign key. Then you're doing the exact same thing for the recipient.
This will allow you to access your Sender and Recipient, both instances of the User model, through an instance of the Message model, like this:
Here is your User Model:
Here you are creating a property on the User Model named :sent_messages, specifying that this property is related to the Message Model, and that the foreign key on the Message model which relates to this property is called 'sender_id'. Then you are doing the same thing for received messages.
This allows you to get all of a users sent or received messages by doing something like this:
Doing either of these will return an array of instances of the Message model.
在迁移中,为每种用户创建两个不同的列。例如:
然后在模型中,这就是将每列映射到 User 类的逻辑发生的地方:
当然,使用您自己的单词来表示
sender
和receiver
,但是 Rails会自动将sender
关联到sender_id
列(receiver
的逻辑相同),然后您将能够与两个用户
进行交互user.sender
和用户.接收者
。In the migration, create two different columns for each kind of user. For example:
Then in the model, that's where the logic to map each column to the User class happens:
Of course, use your own words for
sender
andreceiver
, but Rails will automatically associatesender
to thesender_id
column (and the same logic forreceiver
)You will then be able to interact with both user
user.sender
anduser.receiver
.