於是我開始設置一個的has_many:通過多態關聯在我的應用程序。該模型如下所示:多態的has_many:通過NoMethodError:對於無未定義的方法`克拉斯: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.collections
和Album.first.collections
在rails控制檯中調用時會返回以下錯誤。
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'
有人能告訴我我在這裏弄錯了什麼。它好像沒有看到歌曲與收藏的關係?我只是不知道我做錯了什麼。
我認爲'通過::collectable'應該是'::collectable'。 –
工作@Damien!非常感謝。我可以問你知道嗎?它是否記錄在某處,軌道4的變化? – JonathanSimmons
太棒了!是的,它在Rails指南中有記錄:http://guides.rubyonrails.org/association_basics.html#polymorphic-associations。儘管您可能涵蓋了大部分內容,但絕對值得通過指南多次運行。 –