そこで、アプリで has_many :through ポリモーフィック アソシエーションのセットアップを開始しました。モデルは次のようになります。
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 が属するレコード (曲またはアルバム) を返す
私の問題は、特定のアルバムまたは曲が属するすべてのコレクションを見つけようとするときに発生します。
Rails コンソールで呼び出すと、次のエラーが返されますSong.first.collections
。Album.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'
誰かが私がここで間違っていることを教えてもらえますか? 曲とコレクションとの関係が見られないかのように?何を間違えたのかわからないだけです。