0

그래서 내 응용 프로그램에서 has_many : through polymorphic association을 설정하기 시작했습니다. 모델은 다음과 같이 :다형성 has_many : through NoMethodError : 정의되지 않은 메소드 'klass'for nil : NilClass

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 

이 날 다음 호출을 수행 할 수 있습니다

=>이 첫 번째 컬렉션의 노래의 배열을 반환

Collection.first.songs Collection.first.albums =>를위한 앨범의 배열을 반환 때 첫 컬렉션 이 Collectionitem.first.collection는 =>이>이 Collectionitem가 속한 레코드 (노래 나 앨범을) 반환 = Collectionitem.first.collectable이 Collectionitem가 속한 컬렉션을 반환

내 문제는 온다 특정 앨범이나 노래가 속한 모든 컬렉션을 찾으십시오.

Song.first.collectionsAlbum.first.collections은 모두 레일 콘솔에서 호출 할 때 다음 오류를 반환합니다.

Song.first.collections 
Song Load (0.2ms) SELECT "songs".* FROM "songs" ORDER BY "songs"."id" ASC LIMIT 1 
NoMethodError: undefined method `klass' for nil:NilClass 
    from /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' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `collect' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `source_reflection' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:579:in `derive_class_name' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:133:in `class_name' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:178:in `klass' 
    from /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' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `collect' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:420:in `source_reflection' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/reflection.rb:557:in `check_validity!' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:25:in `initialize' 
    from /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' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `new' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `association' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-4.0.0/lib/active_record/associations/builder/association.rb:70:in `collections' 
    from (irb):3 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start' 
    from /Users/jonathan/.rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>' 
    from bin/rails:4:in `require' 

누군가 내가 뭘 잘못했는지 말해 줄 수 있습니까? 그것은 마치 콜렉션과 노래의 관계가 보이지 않는 것입니까? 나는 내가 뭘 잘못했는지 모른다.

has_many :collectionitems, through: :collectable 

에 :

has_many :collectionitems, as: :collectable 

이 예상대로 작동 everythign 위치에 일단 @damien 코멘트에서 지적

+2

'through : : collectable'은'as : : collectable'이어야한다고 생각합니다. –

+0

그 덕분에 @Damien! 정말 고마워. 어떻게 알았는지 물어봐도 될까요? 레일 4가 어딘가에 문서화되어 있습니까? – JonathanSimmons

+0

좋아요! 예, 레일스 가이드 http://guides.rubyonrails.org/association_basics.html#polymorphic-associations에 문서화되어 있습니다. 비록 당신이 대부분의 일들을 다룰지라도, 그것은 가이드들을 몇 번 달려 볼 가치가 있습니다. –

답변

2

으로 나는 변화 할 필요가 있었다.

다시 한번 감사드립니다. @damien!

+0

나는 비슷한 문제가 있지만, 어디에서 볼 수있는 것이 아니다 : collectable은 당신의 질문에 클래스로 언급된다. – csakon

+0

: collectable은 다형성 할당의 클래스가 아닙니다. – JonathanSimmons

관련 문제