Ruby on Rails: two has_many same_models :through => different_model -


i'd create 2 has_many on same model goes through different model (which join table)

here's 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 this:

user.first.contents 

if this, works. problem second has_many :contents get's called.

now solve creating method this:

def contents     (dises + facs).map(&:contents).flatten end 

however, loose contents scopes if above because contents becomes simple array.

is there way around this? maybe i'm using wrong approach. there way?

thanks

if dis , fac models not different collapse them single set of models , introduce 'type' attribute lets discriminate between these different types of content. won't have issue when trying query (it's going worse want write more complex queries) , it'll scale when want add more content types.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -