ruby on rails - Polymorphic has_many :through NoMethodError: undefined method `klass' for nil:NilClass -


so started setup has_many :through polymorphic association in app. models following:

class song < activerecord::base   has_many :collections, through: :collectionitems   has_many :collectionitems, through: :collectable end  class album < activerecord::base   has_many :collections, through: :collectionitems   has_many :collectionitems, through: :collectable end  class collection < activerecord::base   has_many :albums, through: :collectionitems, source: :collectable, source_type: "album"   has_many :songs, through: :collectionitems, source: :collectable, source_type: "song"   has_many :collectionitems end  class collectionitem < activerecord::base   belongs_to :collection   belongs_to :collectable, polymorphic: true end 

this allows me following calls:

collection.first.songs => returns array of songs first collection collection.first.albums => returns array of albums first collection collectionitem.first.collection => returns collection collectionitem belongs collectionitem.first.collectable => returns record collectionitem belongs (song or album)

my problem comes when try find collections specific album or song belongs to.

both song.first.collections , album.first.collections return following error when called in rails console.

song.first.collections song load (0.2ms)  select "songs".* "songs" order "songs"."id" asc limit 1 nomethoderror: undefined method `klass' nil:nilclass     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `block in source_reflection'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `collect'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `source_reflection'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:579:in `derive_class_name'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:133:in `class_name'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:178:in `klass'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `block in source_reflection'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `collect'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `source_reflection'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:557:in `check_validity!'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:25:in `initialize'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations/has_many_through_association.rb:9:in `initialize'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `new'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `association'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations/builder/association.rb:70:in `collections'     (irb):3     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'     /users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'     bin/rails:4:in `require' 

can tell me i've gotten wrong here. if doesn't see songs relationship collection? don't know i've done wrong.

as @damien pointed out in comment needed change:

has_many :collectionitems, through: :collectable 

to:

has_many :collectionitems, as: :collectable 

once in place everythign worked expected.

thanks again @damien!


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -