私人消息系统的数据模型

发布于 2024-07-17 05:42:37 字数 574 浏览 1 评论 0原文

我正在为我的网络应用程序编写一个私人消息系统。 只需将其视为与在 Facebook 或 Twitter 等典型社交网站上发送私信,甚至通过 Hotmail 发送电子邮件相同的操作。

I have come up with the following migration so far:
class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.integer :sender_id, :recipient_id
      t.string :title
      t.text :body
      t.boolean :read
      t.timestamps
    end
  end

  def self.down
    drop_table :messages
  end
end

但是,sender_id 和recipient_id 都引用同一个字段,即角色模型中的id 字段。 我必须进行哪些更改才能让解释器知道它指的是该字段。 我还需要进行其他更改吗,例如连接表?

I am writing a private messaging system for my web app. Just think of it as doing the same thing as sending PMs on your typical social networking website like Facebook or Twitter, or even sending an e-mail through Hotmail.

I have come up with the following migration so far:
class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.integer :sender_id, :recipient_id
      t.string :title
      t.text :body
      t.boolean :read
      t.timestamps
    end
  end

  def self.down
    drop_table :messages
  end
end

However, the sender_id and recipient_id both refer to the same field, which is the id field in the Role model. What changes do I have to make so the interpreter knows it's referring to that field. Are there other changes I have to make such as join tables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半岛未凉 2024-07-24 05:42:37

如果我正确地阅读了你的问题,你应该使用belongs_to的class_name选项来更改你的模型:

belongs_to :sender, :class_name => 'Role', :foreign_key => 'sender_id'
belongs_to :recipient, :class_name => 'Role', :foreign_key => 'recipient_id'

不过,我认为外键是推断出来的,所以你应该能够将它们保留。

If I'm reading your question correctly, you should make the change in your model using the class_name option for belongs_to:

belongs_to :sender, :class_name => 'Role', :foreign_key => 'sender_id'
belongs_to :recipient, :class_name => 'Role', :foreign_key => 'recipient_id'

I think the foreign keys are inferred, though, so you should be able to leave those off.

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