0

我正在为我的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' 

我觉得外键推断,虽然如此,你应该能够:

+1

请将您的代码放入格式化的代码块中。这会让人们更容易阅读和理解你的问题。 (突出显示您的代码并键入Ctrl-K) – 2009-04-18 15:26:44

回答

1

如果我正确地读你的问题,你应该使用belongs_to的该CLASS_NAME选项使模型中的变化让他们离开。

+0

belongs_to上的默认外键是association_name_id,所以它会在您的示例中猜出sender_id和recipient_id。 – 2009-04-18 22:26:04

+0

应该将外键替换为“role_id”吗?我希望它被命名为sender_id,但我如何确定sender_id实际上是指role_id? – alamodey 2009-04-19 03:09:01