我知道有很多关于此类错误的主题,你可以肯定我已经阅读了所有这些但仍然无法弄清楚为什么这不起作用。我仍然得到这个错误:
HasManyThroughAssociationNotFoundError:找不到关联
以下是我的模特:
#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
)
ruby控制台中的完全错误:
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/ruby-2.2.1@matchit/gems/activerecord-4.2.4/lib/active_record/reflection.rb:828:in `check_validity!'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activerecord-4.2.4/lib/active_record/associations/association.rb:25:in `initialize'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activerecord-4.2.4/lib/active_record/associations/has_many_through_association.rb:10:in `initialize'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activerecord-4.2.4/lib/active_record/associations.rb:162:in `new'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activerecord-4.2.4/lib/active_record/associations.rb:162:in `association'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activerecord-4.2.4/lib/active_record/associations/builder/association.rb:115:in `collections'
from (irb):78
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/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/ruby-2.2.1@matchit/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/eqal/.rvm/gems/ruby-2.2.1@matchit/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 >
我感到非常绝望,所以我希望有人知道这里出了什么问题。
非常感谢!
答案 0 :(得分:1)
Rails约定期望您的直通表命名为:collections_releases
。而你的模型CollectionsRelease
。当然,如果你想保留单数collection_releases
名称,你可以覆盖。