【问题标题】:Solidus: sorting query with many relationsSolidus:具有许多关系的排序查询
【发布时间】:2018-01-30 14:49:50
【问题描述】:

我正在学习一些 Solidus(Spree 的分支)。我按照readme

构建了沙盒

现在我想在 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(cache_key_for_products)

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

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

【问题讨论】:

    标签: ruby-on-rails spree solidus


    【解决方案1】:

    希望这对你有帮助。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      相关资源
      最近更新 更多