ruby on rails - HABTM relationships with polymorphism -
- i've got person can linked many structures (structure polymorphic)
- i've got venue, can have many people, structure.
- i've got journal, can have many people, structure.
here modelization :
class venue < activerecord::base   has_many :structure_people, :as => :structure   has_many :people, :through => :structure_people end  class journal < activerecord::base   has_many :structure_people, :as => :structure   has_many :people, :through => :structure_people end  class person < activerecord::base   has_many :structure_people   has_many :structures, :through => :structure_people end  class structureperson < activerecord::base   belongs_to :structure, polymorphic: true   belongs_to :person end my problem :
- when try people on venue or on journal, works. cool :)
but
- when try structures on person, i've got error : - activerecord::hasmanythroughassociationpolymorphicsourceerror: cannot have has_many :through association 'person#structures' on polymorphic object 'structure#structure'. 
anyone me solve ?
thanks lot.
christophe
i think it's restriction of rails, because has_many association guess class_name automatically. polymorphic association may returns multiple class_name. mind use this:
class person < activerecord::base   has_many :structure_people   has_many :venues, :through => :structure_people   #journal same. end  class structureperson < activerecord::base   belongs_to :structure, polymorphic: true   belongs_to :venue, :foreign_key => 'structure_id', :conditions => {:structure_type => 'venue'}   belongs_to :person end although ugly solution...
i think can choose alternative way.
class person < activerecord::base   has_many :structure_people    def structures     structure_people.map(&:structure)   end end you can't chaining function of has_many, can polymorphic structures :)
Comments
Post a Comment