0

我正在学习一些 Solidus(Spree 的分支)。我按照自述文件构建了沙箱

现在我想在 Product 中添加一个范围来构建一个查询,对评估每个变体的产品进行排序。对于每种产品,我想评估每种产品的最低变体价格。

例如,如果我有三个产品

a.price = 2
b.price = 3
c.price = 4

a 和 b 只有主变体,c 有两个变体

c.variants.first.price = 4
c.variants.last.price = 1

订购的产品应该是

c, a, b

我设法在使用数组的 home#index 中做到了这一点,但是我破坏了home /index.html.erb方法中的缓存(cache_key_for_products) 。

所以我想做同样的事情,但使用ActiveRecord_Relation

我不知道如何使用 rails 和solidus 构建一个好的查询,任何帮助将不胜感激。

4

1 回答 1

0

希望这可能对您有所帮助。

class Product < ApplicationRecord 
 has_many :variants,  dependent: :destroy
end

class Variant < ApplicationRecord
belongs_to :product
end


def index
 @products = Product.left_outer_joins(:variants).order("case when variants.id is null then products.price else variants.price end asc")
end
于 2018-01-30T19:45:03.637 回答