Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 同一个表的多对多关系(Ruby on Rails)_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 同一个表的多对多关系(Ruby on Rails)

Ruby on rails 同一个表的多对多关系(Ruby on Rails),ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在开发一个Rails应用程序,它有一个“产品”模型。我希望能够将产品相互关联起来。示例:产品1与产品2、产品3相关,反之亦然。我如何在Rails中实现这一点?我在考虑一个联接表,但由于我使用的是同一个表作为关系点,我不确定该如何工作。你说得对,你需要一个联接表。它需要两个字段,这两个字段都是返回products表的外键 所以类似于ProductRelation表,其中包含FirstProduct和SecondProduct字段(这些字段可能有更好的名称),然后您知道FirstProduct与

我正在开发一个Rails应用程序,它有一个“产品”模型。我希望能够将产品相互关联起来。示例:产品1与产品2、产品3相关,反之亦然。我如何在Rails中实现这一点?我在考虑一个联接表,但由于我使用的是同一个表作为关系点,我不确定该如何工作。

你说得对,你需要一个联接表。它需要两个字段,这两个字段都是返回products表的外键


所以类似于ProductRelation表,其中包含FirstProduct和SecondProduct字段(这些字段可能有更好的名称),然后您知道FirstProduct与SecondProduct相关。。。那么,您对相关产品的查询就相当简单了。

未经测试,从内存中,我想您会想要这样的东西:

class ProductLink < ActiveRecord::Base
  belongs_to :parent_product, :class_name => 'Product'
  belongs_to :child_product, :class_name => 'Product'
end

class Product < ActiveRecord::Base
  has_many :parent_links, :class_name => 'ProductLink', :foreign_key => :parent_product_id
  has_many :child_links, :class_name => 'ProductLink', :foreign_key => :child_product_id
end
class-ProductLink“产品”
属于:子产品,:类名称=>“产品”
结束
类产品'ProductLink',:foreign\u key=>:parent\u product\u id
有很多:子链接,:class\u name=>'ProductLink',:foreign\u key=>:child\u product\u id
结束
ProductLink(或您选择的任何名称)将能够包含一个或多个描述关系的附加字段


虽然我想这可能需要一个“products\u products”表,这可能会有点压力,但您可以让它与一起工作。

使用
acts\u as\u follower
gem。它在以下关系方面非常灵活,并提供了通用的以下语义


非常简单,而且效果非常好。用它来表示产品1跟随产品2/3等等。

试试Acts\u as\u嵌套插件

或许Ryan Bates的屏幕广播也能帮助您:


我发现这个答案最有用:

由此,我成功地实现了模型与自身的双向多对多关联。在您的情况下,它将如下所示:

class Product < ActiveRecord::Base
   ...

   has_many :parent_product_map, class_name: 'ProductMap', foreign_key: 'child_product_id'
   has_many :parent_products, through: :parent_product_map, source: :parent_product, class_name: 'Product'
   has_many :child_product_map, class_name: 'ProductMap', foreign_key: 'parent_product_id'
   has_many :child_products, through: :child_product_map, source: :child_product, class_name: 'Product'
   ...
end

class ProductMap < ActiveRecord::Base
   attr_accessible :child_product_id, :id, :parent_product_id
   belongs_to :child_product, foreign_key: 'child_product_id', class_name: 'Product'
   belongs_to :parent_product, foreign_key: 'parent_product_id', class_name: 'Product'
end

class CreateProductMap < ActiveRecord::Migration
   def change
      create_table :product_maps do |t|
      t.integer :id
      t.timestamps
      t.integer :child_product_id
      t.integer :parent_product_id
    end
 end
类产品
从内存中也可以看到,但是你不会使用has\u many:parent\u products,:通过'ProductLink'和has\u many:child\u products,:通过'ProductLink'?我相信上面正确的URL应该是:不,应该是,更正了我的帖子