2015-09-26 50 views
0

我知道有很多關於這種類型的錯誤的主題,你可以肯定,我讀了所有它,但仍然無法弄清楚爲什麼這不起作用。我仍然得到這個錯誤:Rails 4:有很多通過關聯錯誤

HasManyThroughAssociationNotFoundError: Could not find the association

這裏是我的模型:

#collection.rb 
class Collection < ActiveRecord::Base 
belongs_to :user 
has_many :collection_releases 
has_many :releases, :through => :collection_releases 
end 

#release.rb 
class Release < ActiveRecord::Base 
belongs_to :artist 
belongs_to :label 

has_many :collection_releases 
has_many :collections, :through => :collection_releases 

has_many :track_releases 
has_many :tracks, :through => :track_releases 

validates :title, presence: true 
end 

#collection_release 
class CollectionRelease < ActiveRecord::Base 
belongs_to :collection 
belongs_to :release 
end 

我不明白,當我做了錯誤collection.releases - 我只得到當我試圖將錯誤獲得其中釋放關聯(release.collections

完全錯誤的紅寶石控制檯的所有集合:

2.2.1 :077 > co.releases 
=> #<ActiveRecord::Associations::CollectionProxy [#<Release id: 1, created_at: "2015-09-25 14:38:59", updated_at: "2015-09-25 14:38:59", artist_id: 1, label_id: 1, title: "First Release of BF on Larj", year: nil, country: "Germany">]> 
2.2.1 :078 > trelease.collections 
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :collection_releases in model Release 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activerecord-4.2.4/lib/active_record/reflection.rb:828:in `check_validity!' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activerecord-4.2.4/lib/active_record/associations/association.rb:25:in `initialize' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activerecord-4.2.4/lib/active_record/associations/has_many_through_association.rb:10:in `initialize' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activerecord-4.2.4/lib/active_record/associations.rb:162:in `new' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activerecord-4.2.4/lib/active_record/associations.rb:162:in `association' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activerecord-4.2.4/lib/active_record/associations/builder/association.rb:115:in `collections' 
    from (irb):78 
    from /Users/eqal/.rvm/gems/[email protected]/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
    from /Users/eqal/Documents/ror/matchit/bin/rails:8:in `<top (required)>' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency' 
    from /Users/eqal/.rvm/gems/[email protected]/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load' 
    from /Users/eqal/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
    from /Users/eqal/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
    from -e:1:in `<main>'2.2.1 :079 > 

我感到非常絕望,所以我希望有人知道這裏出了什麼問題。

非常感謝!

+1

你能發佈問題中的完整錯誤嗎? – Pavan

+0

錯誤消息必須指向代碼中的特定行。你可以在某些情況下顯示該行嗎? – lurker

+0

我在通過'rails console'檢查我的模型關聯時出現這個錯誤。 我嘗試過'release.collections'(釋放是第一次正確實例化)。然後我得到錯誤: trelease.collections ActiveRecord :: HasManyThroughAssociationNotFoundError:在模型中找不到關聯:collection_releases發佈 – backfloep

回答

1

Rails約定期望您的表格被命名爲:collections_releases。和你的模型CollectionsRelease。當然,如果你想保持單數collection_releases的名字,你可以重寫。

+0

非常感謝!不幸的是,我不知道這個命名約定...... – backfloep