根据Spree中字段的值添加新的搜索字段和过滤结果

时间:2015-02-11 13:07:59

标签: ruby-on-rails spree

我想filter the orders使用stock_location字段。我已将字段添加到过滤器部分:

# app/overrides/admin.rb
Deface::Override.new(:virtual_path => "spree/admin/orders/index",
                     :name => "stock_locations",
                     :insert_top => "div.omega.four.columns",
                     :text => "<%= label_tag :q_line_items_variant_stock_locations_id_eq, 'Stock Locations' %><%= f.select :line_items_variant_stock_locations_id_eq, Spree::StockLocation.pluck(:id, :name).collect { |id, name| [name.strip, id]}, {:include_blank => true}, :class => 'select2 js-filterable'")

在UI中,正在传递股票位置的值,但结果显示所有订单。

生产日志

==> log/thin.3001.log <==
Started GET "/admin/orders?utf8=%E2%9C%93&q%5Bcreated_at_gt%5D=&q%5Bcreated_at_lt%5D=&q%5Bstate_eq%5D=&q%5Bnumber_cont%5D=&q%5Bemail_cont%5D=&q%5Bbill_address_firstname_start%5D=&q%5Bbill_address_lastname_start%5D=&q%5Bline_items_variant_stock_locations_id_eq%5D=10&q%5Bcompleted_at_not_null%5D=0&q%5Bpromotions_id_in%5D=&button=" for 127.0.0.1 at 2015-02-12 07:57:41 +0000
Processing by Spree::Admin::OrdersController#index as HTML
  Parameters: {"utf8"=>"✓", "q"=>{"created_at_gt"=>"", "created_at_lt"=>"", "state_eq"=>"", "number_cont"=>"[FILTERED]", "email_cont"=>"", "bill_address_firstname_start"=>"", "bill_address_lastname_start"=>"", "line_items_variant_stock_locations_id_eq"=>"4", "completed_at_not_null"=>"0", "promotions_id_in"=>""}, "button"=>""}
  Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/orders/index.html.erb within spree/layouts/admin (212.6ms)
  Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/shared/_translations.html.erb (4.7ms)
  Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/shared/_head.html.erb (6.5ms)

在我的控制台中,我看到的数据少于我从UI看到的数据。基本上,在UI中,似乎过滤不起作用。

2.1.2 :005 > Spree::Order.ransack(line_items_variant_stock_locations_id_eq: 4).result.to_a.count
 => 156
2.1.2 :015 > y Spree::Order.ransackable_associations
---
- user
- created_by
- approver
- bill_address
- ship_address
- state_changes
- line_items
- payments
- return_authorizations
- adjustments
- line_item_adjustments
- shipment_adjustments
- inventory_units
- products
- variants
- promotions
- shipments
 => nil

1 个答案:

答案 0 :(得分:2)

我曾经使用过错误的联想。工作代码是:

Deface::Override.new(:virtual_path => "spree/admin/orders/index",
                     :name => "stock_locations",
                     :insert_top => "div.omega.four.columns",
                     :text => "<%= label_tag :q_shipments_stock_location_id_eq, 'Stock Locations' %><%= f.select :shipments_stock_location_id_eq, Spree::StockLocation.pluck(:id, :name).collect { |id, name| [name.strip, id]}, {:include_blank => true}, :class => 'select2 js-filterable'")

这就是我进入最终Ransack属性命名的方式,它在内部执行查询。

2.1.2 :016 > o = Spree::Order.find_by_number('R482860025')
 => #<Spree::Order id: 144, ...>
2.1.2 :020 > o.shipments.first.stock_location.name
 => "Warehouse"
2.1.2 :020 > o.shipments.first.stock_location.id
 => "4"