我在使用 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/