简体   繁体   English

Rails,ActiveModel模型关联?

[英]Rails, ActiveModel Model Associations?

I have the follow two models: 我有以下两种模式:

User
Mentions (user_id, mentioned_by)

@mention.user gives me the creator, but I need the user who was mention. @take.user给了我创建者,但我需要提到的用户。 I would like to do: @mention.mentioner which gets the user_id with mentioned_by 我想做的是:@ mention.mentioner,它使用提到的by_by获取user_id

How do I set that relationship in the models? 如何在模型中设置该关系? I tried: 我试过了:

User.rb:
  has_many :mentions


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

But that errors. 但是那个错误。 Ideas? 想法? Thanks 谢谢

In mention.rb , try this: mention.rb ,试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM