I'd like to create 2 has_many on the same model but that goes through a different model (which is a join table)
Here's the code:
class Content
belongs_to :dis
belongs_to :fac
has_many :dis_representations, :through => :dis
has_many :fac_representations, :through => :fac
end
class Fac
has_many :fac_representations
has_many :contents
end
class Dis
has_many :dis_representations
has_many :contents
end
class DisRepresentation
belongs_to :user, :foreign_key => "e_user_id"
belongs_to :dis
end
class FacRepresentation
belongs_to :user, :foreign_key => "e_user_id"
belongs_to :fac
end
class User
has_many :dis_representations, :foreign_key => "e_user_id"
has_many :fac_representations, :foreign_key => "e_user_id"
has_many :dises, :through => :dis_representations
has_many :facs, :through => :fac_representations
has_many :contents, :through => :dises, :source => :contents
has_many :contents, :through => :facs :source => :contents
end
Now I'd like to do this:
User.first.contents
If I do this, it almost works. The only problem is that only the second has_many :contents get's called.
Now I could solve it by creating a method like this:
def contents
(dises + facs).map(&:contents).flatten
end
However, I loose all contents scopes if I do the above because contents becomes a simple array.
Is there any way around this? Maybe I'm using a completely wrong approach. Is there another way?
Thanks