0

Web アプリ用のプライベート メッセージング システムを作成しています。Facebook や Twitter などの一般的なソーシャル ネットワーキング Web サイトで 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 フィールドである同じフィールドを参照します。インタープリターがそのフィールドを参照していることを認識するために、どのような変更を加える必要がありますか。テーブルの結合など、他に必要な変更はありますか?

4

1 に答える 1

1

私があなたの質問を正しく読んでいれば、belongs_to の class_name オプションを使用してモデルを変更する必要があります。

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

ただし、外部キーは推測されると思うので、それらをオフにしておくことができるはずです。

于 2009-04-18T22:05:45.133 に答える