基于created_at在Spree中对产品列表进行排序

Hus*_*Ali 1 ruby spree ruby-on-rails-3

我正在使用狂欢,我想根据产品对产品列表进行排序created_at.

我试图找到覆盖lib/scopes/product.rb下的spree默认范围但无法找到它的方法.

我想在公共面板上列出最近创建的产品.我怎么能用狂欢来做呢?

j15*_*15e 13

第一个答案将打破管理面板产品版本和其他东西在spree 1.1-stable中.

模糊列名:created_at

您可以通过使用以下命令指定表名来解决此问题:

 Product.class_eval do
    default_scope order("spree_products.created_at DESC")
 end
Run Code Online (Sandbox Code Playgroud)

但我认为最好的解决方案是修补公共产品控制器或视图,而不是模型本身,因为default_scope可能无处不在,并且切换/删除default_scope中定义的订单,您必须调用.re order()

也许正因为如此,SpreeCommerce文档特别建议您不要在产品范围中添加订单:

资料来源:http://guides.spreecommerce.com/scopes_and_groups.html#modifying-available-scopes

因此,我认为在不破坏Spree核心产品模型的情况下执行此操作的正确方法是覆盖产品模板:

覆盖views/spree/shared/_products.html.rb

更换

 <% products.each do |product| %>
Run Code Online (Sandbox Code Playgroud)

 <% products.descend_by_updated_at.each do |product| %>
Run Code Online (Sandbox Code Playgroud)

来源:https://groups.google.com/forum/#!topic/spree- user/lW5sGsbMTfM

为我工作™