私人消息系统的数据模型
我正在为我的网络应用程序编写一个私人消息系统。 只需将其视为与在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确地阅读了你的问题,你应该使用belongs_to的class_name选项来更改你的模型:
不过,我认为外键是推断出来的,所以你应该能够将它们保留。
If I'm reading your question correctly, you should make the change in your model using the class_name option for belongs_to:
I think the foreign keys are inferred, though, so you should be able to leave those off.