Rails,ActiveModel模型关联?

时间:2011-10-31 23:02:42

标签: ruby-on-rails ruby-on-rails-3 activerecord

我有以下两种模式:

User
Mentions (user_id, mentioned_by)

@take.user给了我创建者,但我需要提到的用户。我想这样做:@take.mentioner获取带有submitted_by的user_id

如何在模型中设置该关系?我试过了:

User.rb:
  has_many :mentions


Mention.rb
belongs_to :user
belongs_to :user, :as => "mentioner", :foreign_key => "mentioned_by"

但那个错误。想法?感谢

1 个答案:

答案 0 :(得分:3)

mention.rb中,试试这个:

belongs_to :user
belongs_to :mentioner, :class_name => "User", :foreign_key => "mentioned_by"