Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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 5.1中的自连接关联从多个关联中获取对象_Ruby On Rails_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 通过Rails 5.1中的自连接关联从多个关联中获取对象

Ruby on rails 通过Rails 5.1中的自连接关联从多个关联中获取对象,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我有两种型号,产品和变体。产品模型与变化模型有许多关系。我还有一个match模型,它与产品模型具有自连接关系 我想做的是从变化模型中得到一个与匹配产品有关系的对象 product.rb class Product < ApplicationRecord ... has_many :variations, -> { order(:order) }, dependent: :destroy has_and_belongs_to_many :categories has_a

我有两种型号,
产品
变体
。产品模型与变化模型有许多关系。我还有一个
match
模型,它与产品模型具有自连接关系

我想做的是从变化模型中得到一个与匹配产品有关系的对象

product.rb

class Product < ApplicationRecord
  ...
  has_many :variations, -> { order(:order) }, dependent: :destroy
  has_and_belongs_to_many :categories
  has_and_belongs_to_many :tags
  has_many :matches
  has_many :matched_products, through: :matches
  ...
end
class Variation < ApplicationRecord
  ...
  VARIATION_ORDER = %w[1 2 3 4 5 6 7 8 9 10]
  validates :product_id, presence: true
  has_and_belongs_to_many :categories
  has_and_belongs_to_many :tags
  has_many :products
  belongs_to :product
  ...
end
class Match < ActiveRecord::Base
  belongs_to :product
  belongs_to :matched_product, class_name: 'Product'
end
类产品{order(:order)},依赖::destroy
_和_是否属于多个类别
_和_属于_多个:标记吗
你有很多比赛吗
通过::matches,拥有多个:匹配的产品
...
结束
变体.rb

class Product < ApplicationRecord
  ...
  has_many :variations, -> { order(:order) }, dependent: :destroy
  has_and_belongs_to_many :categories
  has_and_belongs_to_many :tags
  has_many :matches
  has_many :matched_products, through: :matches
  ...
end
class Variation < ApplicationRecord
  ...
  VARIATION_ORDER = %w[1 2 3 4 5 6 7 8 9 10]
  validates :product_id, presence: true
  has_and_belongs_to_many :categories
  has_and_belongs_to_many :tags
  has_many :products
  belongs_to :product
  ...
end
class Match < ActiveRecord::Base
  belongs_to :product
  belongs_to :matched_product, class_name: 'Product'
end
class Variation
match.rb

class Product < ApplicationRecord
  ...
  has_many :variations, -> { order(:order) }, dependent: :destroy
  has_and_belongs_to_many :categories
  has_and_belongs_to_many :tags
  has_many :matches
  has_many :matched_products, through: :matches
  ...
end
class Variation < ApplicationRecord
  ...
  VARIATION_ORDER = %w[1 2 3 4 5 6 7 8 9 10]
  validates :product_id, presence: true
  has_and_belongs_to_many :categories
  has_and_belongs_to_many :tags
  has_many :products
  belongs_to :product
  ...
end
class Match < ActiveRecord::Base
  belongs_to :product
  belongs_to :matched_product, class_name: 'Product'
end
类匹配
products/show.html.erb 这将为我提供匹配产品的name对象,但我还希望获得与匹配产品关联的变体的名称

。。。
  • ...
    要获取变体图像的名称,可以执行以下操作:

    c.matched_product.variations.pluck(:image).join(', ')
    


    不要忘记使用
    includes(:variations)
    来避免N+1查询,即:
    @product.matches.includes(:variations)

    A
    product
    有许多
    变体
    ,您想获得什么
    变体
    名称?所有?@fongfan999是的,客户希望显示相关产品的名称以及相关变体的名称和图像。您能给我看一下型号
    图像
    ?图像没有型号,它是变体模型的一部分。变量模型有:name、:image、:body