3
我想在變異的價格小於其比較的價格寫一個ActiveRecord查詢過濾所有的變種。這裏是我的變種型號:如何通過關聯表格上的計算來過濾?
class Variant < ApplicationRecord
has_and_belongs_to_many :comparisons,
class_name: "Variant",
foreign_key: "competitor_variant_id",
association_foreign_key: "account_variant_id",
join_table: "comparisons"
belongs_to :parent, polymorphic: true
end
這裏是我到目前爲止有:
joins(:comparisons).where('variants.price < comparisons_variants.min(:price)').distinct