ruby-on-rails - rails : HasManyThroughAssociationNotFoundError

标签 ruby-on-rails ruby activerecord has-many-through

我在使用 has_many through 关联时遇到问题。

我一直收到这个异常:

Article.find(1).warehouses.build
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :entries in model Article

这些是涉及的模型:

class Article < ActiveRecord::Base
  has_many :warehouses, :through => :entries
end

class Warehouse < ActiveRecord::Base
  has_many :articles, :through => :entries
end

class Entry < ActiveRecord::Base
  belongs_to :article
  belongs_to :warehouse
end

这是我的模式:

create_table "articles", :force => true do |t|
  t.string   "article_nr"
  t.string   "name"
  t.integer  "amount"
  t.string   "warehouse_nr"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "unit"
end

create_table "entries", :force => true do |t|
  t.integer "warehouse_id"
  t.integer "article_id"
  t.integer "amount"
end

create_table "warehouses", :force => true do |t|
  t.string   "warehouse_nr"
  t.string   "name"
  t.integer  "utilization"
  t.datetime "created_at"
  t.datetime "updated_at"
end

最佳答案

你需要添加

has_many :entries

对于您的每个模型,因为 :through 选项只指定了第二个关联,它应该使用它来找到另一边。

关于ruby-on-rails - rails : HasManyThroughAssociationNotFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/944126/

相关文章:

mysql - 具有多个条件的 JOINED 表上的 SQL 查询

ruby-on-rails - ActiveAdmin 批量操作动态表单

ruby-on-rails - 在 ruby​​ on Rails 中按字母顺序对 json 数据进行排序

ruby-on-rails - Ruby on Rails : can I use http basic auth in constraints?

ruby-on-rails - SQL 注入(inject)和 ActiveRecord

ruby-on-rails - 通过比较两个日期范围是否重叠来过滤 Rails 查询

ruby-on-rails - 如何使用 amazon cognito 实现密码过期和密码可存档功能

ruby - Ruby:在具有 “string buffer”时查找文件的音频持续时间

ruby - Selenium RC 与 safari 问题 - Selenium::WebDriver::Error::UnknownError: Executor has not been started yet

ruby-on-rails - 如何优化对数千个 ID 的查询