参考模型编写两次迁移

发布于 2024-12-12 05:20:51 字数 97 浏览 0 评论 0原文

我有一个消息模型(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2024-12-19 05:20:51

这是这个问题的完整答案,以防访问这个问题的人很难将所有内容放在一起(就像我第一次研究这个问题时一样)。

答案的某些部分发生在您的迁移中,某些部分发生在您的模型中:

迁移

class CreateMessages < ActiveRecord::Migration
  create_table :messages do |t|
    def up
      t.references :sender
      t.references :recipient
    end
  end
end

这里您指定此表中有两列将被称为 :sender 和 :recipient 并保存对另一个表的引用。 Rails 实际上会为您创建名为“sender_id”和“recipient_id”的列。在我们的例子中,它们将各自引用 Users 表中的行,但我们在模型中指定,而不是在迁移中。

模型

class Message < ActiveRecord::Base
  belongs_to :sender, :class_name => 'User'
  belongs_to :recipient, :class_name => 'User'
end

这里您将在名为 :sender 的 Message 模型上创建一个属性,然后指定该属性将引用 User 类的实例。 Rails 看到“belongs_to”后,会在数据库中查找名为“sender_id”(我们在上面定义)的列,并使用它来存储外键。然后你就为收件人做同样的事情。

这将允许您通过消息模型的实例访问发送者和接收者(用户模型的两个实例),如下所示:

@message.sender.name
@message.recipient.email

这是您的用户模型:

class User < ActiveRecord::Base
  has_many :sent_messages, :class_name => 'Message', :foreign_key => 'sender_id'
  has_many :received_messages, :class_name => 'Message', :foreign_key => 'recipient_id'
end

在这里,您在用户模型上创建一个名为 :sent_messages 的属性,指定该属性与消息模型相关,并且消息模型上与该属性相关的外键称为“sender_id”。然后你对收到的消息做同样的事情。

这允许您通过执行以下操作来获取所有用户发送或接收的消息:

@user.sent_messages
@user.received_messages

执行其中任一操作将返回消息模型实例的数组。

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

class CreateMessages < ActiveRecord::Migration
  create_table :messages do |t|
    def up
      t.references :sender
      t.references :recipient
    end
  end
end

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

class Message < ActiveRecord::Base
  belongs_to :sender, :class_name => 'User'
  belongs_to :recipient, :class_name => 'User'
end

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:

@message.sender.name
@message.recipient.email

Here is your User Model:

class User < ActiveRecord::Base
  has_many :sent_messages, :class_name => 'Message', :foreign_key => 'sender_id'
  has_many :received_messages, :class_name => 'Message', :foreign_key => 'recipient_id'
end

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:

@user.sent_messages
@user.received_messages

Doing either of these will return an array of instances of the Message model.

雪化雨蝶 2024-12-19 05:20:51

在迁移中,为每种用户创建两个不同的列。例如:

add_column :messages, :sender_id, :integer
add_column :messages, :receiver_id, :integer

然后在模型中,这就是将每列映射到 User 类的逻辑发生的地方:

belongs_to :sender, :class_name => 'User'
belongs_to :receiver, :class_name => 'User'

当然,使用您自己的单词来表示 senderreceiver,但是 Rails会自动将 sender 关联到 sender_id 列(receiver 的逻辑相同),

然后您将能够与两个用户 进行交互user.sender用户.接收者

In the migration, create two different columns for each kind of user. For example:

add_column :messages, :sender_id, :integer
add_column :messages, :receiver_id, :integer

Then in the model, that's where the logic to map each column to the User class happens:

belongs_to :sender, :class_name => 'User'
belongs_to :receiver, :class_name => 'User'

Of course, use your own words for sender and receiver, but Rails will automatically associate sender to the sender_id column (and the same logic for receiver)

You will then be able to interact with both user user.sender and user.receiver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文