我正在为我的web应用程序编写一个私人消息系统。试想一下,像在Facebook或Twitter等典型的社交网站上发送PM一样,或者通过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都引用相同的字段,即Role模型中的id字段。我必须做出什么样的改变,所以解释者知道这是指该领域。是否还有其他更改,例如连接表?
belongs_to :sender, :class_name => 'Role', :foreign_key => 'sender_id'
belongs_to :recipient, :class_name => 'Role', :foreign_key => 'recipient_id'
我觉得外键推断,虽然如此,你应该能够:
请将您的代码放入格式化的代码块中。这会让人们更容易阅读和理解你的问题。 (突出显示您的代码并键入Ctrl-K) – 2009-04-18 15:26:44