Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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 4 HABTM有很多:通过_Ruby On Rails_Ruby_Ruby On Rails 4_Rspec_Factory Bot - Fatal编程技术网

Ruby on rails Rails 4 HABTM有很多:通过

Ruby on rails Rails 4 HABTM有很多:通过,ruby-on-rails,ruby,ruby-on-rails-4,rspec,factory-bot,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,Factory Bot,团队成员,正在为Rails 4协会的一个非常特殊的(新手)情况寻求帮助。 我们有3种型号: class Brand < ActiveRecord::Base has_many :lines, dependent: :destroy has_many :products, through: :lines, dependent: :destroy end class Line < ActiveRecord::Base belongs_to :brand has_and_belo

团队成员,正在为Rails 4协会的一个非常特殊的(新手)情况寻求帮助。 我们有3种型号:

class Brand < ActiveRecord::Base
 has_many :lines, dependent: :destroy
 has_many :products, through: :lines, dependent: :destroy
end

class Line < ActiveRecord::Base
 belongs_to :brand
 has_and_belongs_to_many :products
end

class Product < ActiveRecord::Base
 has_and_belongs_to_many :lines
 has_many :brands, through: :lines
end
我们已经对这个异常进行了研究,检查了Rails API,但运气不好,发现的示例显示了不同的模型配置。这种方法缺少什么


感谢你们的帮助

在我看来,应该是这样的:

class Brand < ActiveRecord::Base
 has_many :lines, dependent: :destroy
 has_many :products, through: :lines, dependent: :destroy
end

class Line < ActiveRecord::Base
 belongs_to :brand
 has_and_belongs_to_many :products
end

class Product < ActiveRecord::Base
 belongs_to :brand, through: :line
 has_and_belongs_to_many :lines
end

我希望这会有所帮助。

您是如何尝试访问的?请再添加一点控制器代码。我以前从未在直通表上看到过habtm。这个中间模型通常用于跟踪其他两个模型之间关联的元数据。因此,我建议将brand_product作为一个完整的表格,并将行分开。@sahil product.brands(符合此产品要求的品牌)/brand.products(此品牌下的产品)一旦某个特定品牌(及其行)被删除,我会尝试删除该品牌的关联。我喜欢这种方法,但我们如何将生产线与产品相结合呢?请记住,一个品牌有许多产品线,然后,产品线有并属于许多产品。。。非常感谢。嘿,谢谢你的帮助。。我还检查了此选项,但看起来无效。。。好啊因此有一些问题。有多少品牌有特定的产品?Fn您如何使用它<代码>@brand.products
class Brand < ActiveRecord::Base
 has_many :lines, dependent: :destroy
 has_many :products, through: :lines, dependent: :destroy
end

class Line < ActiveRecord::Base
 belongs_to :brand
 has_and_belongs_to_many :products
end

class Product < ActiveRecord::Base
 belongs_to :brand, through: :line
 has_and_belongs_to_many :lines
end
create_table :brands , force: true do |t|
  t.string :name
  ...
  t.timestamps null: false    
end

create_table :lines , force: true do |t|
  t.string :name
  t.belongs_to :brand
  ...
  t.timestamps null: false    
end

create_table :products , force: true do |t|
  t.string :name
  ...
  t.timestamps null: false    
end

create_table :line_products, force: true, id: false do |t|
  t.belongs_to :line, index: true
  t.belongs_to :product, index: true
end