Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:HasManyThroughAssociationNotFoundError_Ruby On Rails_Ruby_Activerecord_Has Many Through - Fatal编程技术网

Ruby on rails Rails:HasManyThroughAssociationNotFoundError

Ruby on rails Rails:HasManyThroughAssociationNotFoundError,ruby-on-rails,ruby,activerecord,has-many-through,Ruby On Rails,Ruby,Activerecord,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, :throug

我很难通过协会获得一份工作

我一直得到这样的例外:

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
您需要添加

has_many :entries
has_many :entries
因为:through选项只指定了第二个关联,它应该使用它来查找另一边。

@Meekohi 这意味着您没有入口模型。我自己刚收到错误消息,所以想指出它(由于声誉不好,无法将其作为评论发布)

您需要添加

has_many :entries
has_many :entries
对于每个模型,以及以上都有许多:到,如下所示:

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

class Warehouse < ActiveRecord::Base
  has_many :entries
  has_many :articles, :through => :entries
end
类文章:条目
结束
类仓库:条目
结束

关于如何处理视图和控制器的更详细教程

您还可以在创建表关系中使用t.references:warehouse,:null=>false而不是t.integer“warehouse\u id”。。。现在越来越多的Rail-lish。有了这个更改,我得到了
NameError:uninitialized constant Article::Entry
class Article < ActiveRecord::Base
  has_many :entries
  has_many :warehouses, :through => :entries
end

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