我正在学习一些Solidus (疯狂的叉子)。我在之后构建了沙箱
现在,我想在products中添加一个作用域来构建一个查询,该查询对产品进行排序,评估每个变体。对于每一种产品,我想评估每一种产品的最低变价。
例如,如果我有三个产品
a.price = 2
b.price = 3
c.price = 4A和b只有主变体,c有两个变体。
c.variants.first.price = 4
c.variants.last.price = 1订购的产品应该是
c, a, b我在使用数组的cache(cache_key_for_products)中成功地做到了这一点,但是我破坏了home/index.html.erb方法中的home#index。
所以我也想做同样的事情,但是使用ActiveRecord_Relation
我不知道如何构建一个好的查询与rails和solidus,任何帮助将不胜感激。
发布于 2018-01-30 19:45:03
希望这对你有帮助。
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")
endhttps://stackoverflow.com/questions/48524232
复制相似问题