ruby on rails - Source Reflection Errors with has_many :through -


i'm attempting create system site's users can favorites pages. pages have 2 types, either clubs or sports. so, have 4 models, associated such:

user model:

class user < activerecord::base     ..     has_many :favorites     has_many :sports,    :through => :favorites     has_many :clubs,     :through => :favorites     .. end 

favorites model:

class favorite < activerecord::base     ..      belongs_to :user     belongs_to :favoritable, :polymorphic => true  end 

club model:

class club < activerecord::base     ..      has_many :favorites, :as => :favoritable     has_many :users, :through => :favorites      def to_param       slug     end end 

sport model:

class sport < activerecord::base     ..      def to_param         slug     end      ..      has_many :favorites,   :as => :favoritable     has_many :users,       :through => :favorites      .. end 

essentially, user has_many sports or clubs through favorites, , association between favorites, sports, , clubs polymorphic.

in practice, working way want to, , whole system have designed works. however, i'm using rails_admin on site, , error in 3 places:

  1. when loading dashboard (/admin) first time. if refresh page, works fine.
  2. when loading user model in rails_admin
  3. when loading favorites model in rails_admin

here error message on /admin/user (gist). of errors similar, referencing activerecord::reflection::throughreflection#foreign_key delegated source_reflection.foreign_key, source_reflection nil:.

can point me in right direction can fix this? i've searched over, , asked other programmers/professionals, no 1 spot error in models. much!

alright, well, worked out, , figured i'd post fix in case helps else out in future (no 1 likes finding else same problem , no posted answer).

as turns out, polymorphic has_many :through, there little more configuration needed. user model should have looked this:

class user < activerecord::base     ..     has_many :favorites     has_many :sports, :through => :favorites, :source => :favoritable, :source_type => "sport"     has_many :clubs,  :through => :favorites, :source => :favoritable, :source_type => "club"     .. end 

this answer question polymorphic has_many :through associations helped me figure out.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -