Rachela Meadows
Rachela Meadows

Reputation: 815

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. I would like to do: @mention.mentioner which gets the user_id with mentioned_by

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

Upvotes: 1

Views: 398

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124409

In mention.rb, try this:

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

Upvotes: 3

Related Questions