Let’s say I have 2 tables:
Users
user_id name
Tags
tagger_id tagged_id
This describes a situation where you can
And the models that I’m trying to set up are:
class User < ActiveRecord::Base
has_many :tags, :foreign_key => "tagger_id"
end
class Tag < ActiveRecord::Base
belongs_to :tagger, :class => "User"
belongs_to :tagged, :class => "User"
end
I’m trying to set it up so that when I do:
user.tags
It comes back with a list of User objects. With my current setup, it comes back with just the actual Tag record with ids instead of objects. How do I set it up so it returns a list of User objects?
I tried using:
has_many :tags, :foreign_key => "tagger_id", :source => :tagged
But it didn’t work.
You’re looking for has_many :through.
Then
user.taggedshould give you the list of users you want.