Rails 4: 有很多through association错误
Rails 4: has many through association error
我知道有很多关于此类错误的主题,您可以确定我已经阅读了所有内容,但仍然无法弄清楚为什么这不起作用。我仍然收到此错误:
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
)
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 >
我感到非常绝望,所以我希望有人知道这里出了什么问题。
非常感谢!
Rails 惯例要求您通过 table 命名为:collections_releases
。还有你的模特 CollectionsRelease
。当然,如果您想保留单数 collection_releases
名称,您可以覆盖。
我知道有很多关于此类错误的主题,您可以确定我已经阅读了所有内容,但仍然无法弄清楚为什么这不起作用。我仍然收到此错误:
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
)
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 >
我感到非常绝望,所以我希望有人知道这里出了什么问题。
非常感谢!
Rails 惯例要求您通过 table 命名为:collections_releases
。还有你的模特 CollectionsRelease
。当然,如果您想保留单数 collection_releases
名称,您可以覆盖。