大礼包排序

Sorting in spree

按主价格对产品排序在我的项目中不起作用。 我使用标准狂欢安装。

修改部分taxons_controller.rb显示方法:

@searcher = build_searcher(params.merge(taxon: @taxon.id, include_images: true))
@products = @searcher.retrieve_products
binding.pry
@products = @products.reorder('').send(:descend_by_master_price)

当我在 pry console 中写入 @products.reorder('').send(:descend_by_master_price) 时,我得到以下信息:<Module:0x007f1e0048daa8>:0x3f8f041276b8>.

但是如果我写 Product.all.reorder('').send(:descend_by_master_price) 一切正常。

如果是 @products.reorder('').send(:descend_by_master_price).last,我得到错误:

ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...UB' AND "spree_products"."id" IN (5, 1)  ORDER BY "spree_pri...

所以问题出在 DISTINCT 中...

有人可以帮忙吗?

您需要在 select 子句中包含 ORDER BY 字段。 尝试使用,

@products.select('spree_products.*, spree_prices.amount').reorder('').send(:descend_by_master_price)