Rails的:HasManyThroughAssociationNotFoundError [英] Rails: HasManyThroughAssociationNotFoundError

查看:151
本文介绍了Rails的:HasManyThroughAssociationNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,通过的关联得到一个的has_many工作。

我不断收到此异常:

  Article.find(1).warehouses.build
ActiveRecord的:: HasManyThroughAssociationNotFoundError:找不到关联:模型文章条目
 

这些都是涉及到的型号:

 类文章<的ActiveRecord :: Base的
  的has_many:仓库:通过=> :项
结束

一流的仓储LT;的ActiveRecord :: Base的
  的has_many:文章:通过=> :项
结束

类条目<的ActiveRecord :: Base的
  belongs_to的:文章
  belongs_to的:仓库
结束
 

这是我的架构:

  CREATE_TABLE文章,:力=>真做| T |
  t.stringarticle_nr
  t.string名
  t.integer量
  t.stringwarehouse_nr
  t.datetimecreated_at
  t.datetime的updated_at
  t.integer单位
结束

CREATE_TABLE项,:力=>真做| T |
  t.integerwarehouse_id
  t.integer的article_id
  t.integer量
结束

CREATE_TABLE仓库,:力=>真做| T |
  t.stringwarehouse_nr
  t.string名
  t.integer利用
  t.datetimecreated_at
  t.datetime的updated_at
结束
 

解决方案

您需要添加

 的has_many:项
 

要您的每一个模型,因为:通过选项只是指明它应该用它来寻找另一侧的第二关联

I have problems with getting a has_many through association to work.

I keep getting this exception:

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

These are the models involved:

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

And this is my schema:

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

解决方案

You need to add

has_many :entries

To each of your models, since the :through option just specifies a second association which it should use to find the other side.

这篇关于Rails的:HasManyThroughAssociationNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆