其中查询在rails中无法正常工作

时间:2016-01-09 09:30:56

标签: ruby-on-rails postgresql ruby-on-rails-4 spree

我已经包含了给定的代码

@cod_order = Spree::Order.where(:cod_pay => true)
  Spree::Order Load (4.8ms)  SELECT "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."cod_pay" = 't'
#<ActiveRecord::Relation [#<Spree::Order id: 6028, number: "R054426409", item_total: #<BigDecimal:7fc927bb61b8,'0.549E2',18(18)>, total: #<BigDecimal:7fc927bb5ad8,'0.549E2',18(18)>, state: "complete", adjustment_total: #<BigDecimal:7fc927baf9a8,'0.0',9(18)>, user_id: 1, completed_at: "2016-01-09 07:52:34", bill_address_id: 24857, ship_address_id: 24858, payment_total: #<BigDecimal:7fc927ba7d98,'0.0',9(18)>, shipping_method_id: nil, shipment_state: "pending", payment_state: "balance_due", email: "admin@skinnymint.com", special_instructions: nil, created_at: "2016-01-09 07:52:15", updated_at: "2016-01-09 07:52:34", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7fc927b95530,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7fc927b94f68,'0.0',9(18)>, promo_total: #<BigDecimal:7fc927b94db0,'0.0',9(18)>, channel: "spree", included_tax_total: #<BigDecimal:7fc927b94978,'0.0',9(18)>, item_count: 1, approver_id: nil, approved_at: nil, confirmation_delivered: true, considered_risky: false, guest_token: "0r3KCAINSIGwiX4yCUYMEQ", locale: "en", state_lock_version: 0, cod_pay: true>, #<Spree::Order id: 6025, number: "R803769939", item_total: #<BigDecimal:7fc927b8dc40,'0.699E2',18(18)>, total: #<BigDecimal:7fc927b8d9c0,'0.699E2',18(18)>, state: "complete", adjustment_total: #<BigDecimal:7fc927b8cea8,'0.0',9(18)>, user_id: 1, completed_at: "2016-01-08 14:02:46", bill_address_id: 24793, ship_address_id: 24794, payment_total: #<BigDecimal:7fc927b85dd8,'0.0',9(18)>, shipping_method_id: nil, shipment_state: "pending", payment_state: "balance_due", email: "dins@skinnymint.com", special_instructions: nil, created_at: "2016-01-08 14:02:30", updated_at: "2016-01-08 14:02:46", currency: "SGD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7fc927b77b20,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7fc927b77468,'0.0',9(18)>, promo_total: #<BigDecimal:7fc927b769c8,'0.0',9(18)>, channel: "spree", included_tax_total: #<BigDecimal:7fc927b75ff0,'0.0',9(18)>, item_count: 1, approver_id: nil, approved_at: nil, confirmation_delivered: true, considered_risky: false, guest_token: "0r3KCAINSIGwiX4yCUYMEQ", locale: "en", state_lock_version: 0, cod_pay: true>, #<Spree::Order id: 6023, number: "R290423726", item_total: #<BigDecimal:7fc927b5d4c8,'0.699E2',18(18)>, total: #<BigDecimal:7fc927b5d298,'0.699E2',18(18)>, state: "complete", adjustment_total: #<BigDecimal:7fc927b5c320,'0.0',9(18)>, user_id: 1, completed_at: "2016-01-08 13:59:04", bill_address_id: 24785, ship_address_id: 24786, payment_total: #<BigDecimal:7fc927b5a700,'0.0',9(18)>, shipping_method_id: nil, shipment_state: "pending", payment_state: "balance_due", email: "admin@skinnymint.com", special_instructions: nil, created_at: "2016-01-08 13:41:15", updated_at: "2016-01-08 13:59:04", currency: "SGD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7fc927b640c0,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7fc927b53d60,'0.0',9(18)>, promo_total: #<BigDecimal:7fc927b53900,'0.0',9(18)>, channel: "spree", included_tax_total: #<BigDecimal:7fc927b52460,'0.0',9(18)>, item_count: 1, approver_id: nil, approved_at: nil, confirmation_delivered: true, considered_risky: false, guest_token: "0r3KCAINSIGwiX4yCUYMEQ", locale: "en", state_lock_version: 0, cod_pay: true>, #<Spree::Order id: 6030, number: "R903929411", item_total: #<BigDecimal:7fc927b49428,'0.848E2',18(18)>, total: #<BigDecimal:7fc927b48fc8,'0.299E2',18(18)>, state: "complete", adjustment_total: #<BigDecimal:7fc927b43f50,'-0.549E2',18(18)>, user_id: 1, completed_at: "2016-01-09 08:40:47", bill_address_id: 24865, ship_address_id: 24866, payment_total: #<BigDecimal:7fc927b33e98,'0.0',9(18)>, shipping_method_id: nil, shipment_state: "pending", payment_state: "balance_due", email: "admin@skinnymint.com", special_instructions: nil, created_at: "2016-01-09 08:40:12", updated_at: "2016-01-09 08:40:47", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7fc927b2a280,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7fc927b29cb8,'0.0',9(18)>, promo_total: #<BigDecimal:7fc927b29830,'-0.549E2',18(18)>, channel: "spree", included_tax_total: #<BigDecimal:7fc927b28390,'0.0',9(18)>, item_count: 2, approver_id: nil, approved_at: nil, confirmation_delivered: true, considered_risky: false, guest_token: "0r3KCAINSIGwiX4yCUYMEQ", locale: "en", state_lock_version: 0, cod_pay: true>]>

下一个代码

cod_order = Spree::Order.where(:cod_pay => true).accessible_by(current_ability, :index).unfulfilled.ransack(ransack_hash)
#  Ransack::Search<class: Spree::Order, base: Grouping <combinator: and>>
cod_order.result(distinct: true).page(params[:page]).per(params[:per_page] || Spree::Config[:orders_per_page])
  CACHE (0.0ms)  SELECT  DISTINCT "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."cod_pay" = 't' AND "spree_orders"."state" = 'complete' AND "spree_orders"."payment_state" IN ('paid', 'credit_owed') AND "spree_orders"."shipment_state" IN ('ready')  ORDER BY "spree_orders"."completed_at" DESC LIMIT 15 OFFSET 0
#<ActiveRecord::Relation []>

它给出了空白数组,请指导我为什么给出空白数组以及何时包含给定代码

@orders = @search.result(distinct: true).page(params[:page]).per(params[:per_page] || Spree::Config[:orders_per_page])
  Spree::Order Load (3.6ms)  SELECT  DISTINCT "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."state" = 'complete' AND "spree_orders"."payment_state" IN ('paid', 'credit_owed') AND "spree_orders"."shipment_state" IN ('ready')  ORDER BY "spree_orders"."completed_at" DESC LIMIT 15 OFFSET 0
#<ActiveRecord::Relation [#<Spree::Order id: 6002, number: "R516804873", item_total: #<BigDecimal:7fc926184588,'0.848E2',18(18)>, total: #<BigDecimal:7fc926184268,'0.299E2',18(18)>, state: "complete", adjustment_total: #<BigDecimal:7fc9261777c0,'-0.549E2',18(18)>, user_id: 1, completed_at: "2016-01-07 07:48:56", bill_address_id: 24716, ship_address_id: 24717, payment_total: #<BigDecimal:7fc92616d770,'0.299E2',18(18)>, shipping_method_id: nil, shipment_state: "ready", payment_state: "paid", email: "dinshaw.r@hcl.com", special_instructions: nil, created_at: "2016-01-07 07:44:32", updated_at: "2016-01-07 07:48:56", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7fc92614ad38,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7fc92614a810,'0.0',9(18)>, promo_total: #<BigDecimal:7fc926149ff0,'-0.549E2',18(18)>, channel: "spree", included_tax_total: #<BigDecimal:7fc926148f60,'0.0',9(18)>, item_count: 2, approver_id: nil, approved_at: nil, confirmation_delivered: true, considered_risky: false, guest_token: "0r3KCAINSIGwiX4yCUYMEQ", locale: "en", state_lock_version: 0, cod_pay: false>, #<Spree::Order id: 5995, number: "R803362142", item_total: #<BigDecimal:7fc9261434c0,'0.549E2',18(18)>, total: #<BigDecimal:7fc926142b10,'0.549E2',18(18)>, state: "complete", adjustment_total: #<BigDecimal:7fc926137530,'0.0',9(18)>, user_id: 1, completed_at: "2016-01-07 06:21:24", bill_address_id: 24683, ship_address_id: 24684, payment_total: #<BigDecimal:7fc92612a650,'0.549E2',18(18)>, shipping_method_id: nil, shipment_state: "ready", payment_state: "paid", email: "adminssssss@skinnymint.com", special_instructions: nil, created_at: "2016-01-07 06:19:44", updated_at: "2016-01-07 06:21:24", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7fc926a0b3a0,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7fc926a0af40,'0.0',9(18)>, promo_total: #<BigDecimal:7fc926a0ab08,'0.0',9(18)>, channel: "spree", included_tax_total: #<BigDecimal:7fc926a0a658,'0.0',9(18)>, item_count: 1, approver_id: nil, approved_at: nil, confirmation_delivered: true, considered_risky: false, guest_token: "0r3KCAINSIGwiX4yCUYMEQ", locale: "en", state_lock_version: 0, cod_pay: false>,...]>

我也试过了给定的代码

@cod_order = Spree::Order.where(["cod_pay = ? and payment_state = ?", true, "balance_due"]).accessible_by(current_ability, :index).unfulfilled.ransack(ransack_hash)
Ransack::Search<class: Spree::Order, base: Grouping <combinator: and>>
cod_order =  @cod_order.result(distinct: true).page(params[:page]).per(params[:per_page] || Spree::Config[:orders_per_page])
  Spree::Order Load (4.1ms)  SELECT  DISTINCT "spree_orders".* FROM "spree_orders"  WHERE (cod_pay = 't' and payment_state = 'balance_due') AND "spree_orders"."state" = 'complete' AND "spree_orders"."payment_state" IN ('paid', 'credit_owed') AND "spree_orders"."shipment_state" IN ('ready')  ORDER BY "spree_orders"."completed_at" DESC LIMIT 15 OFFSET 0
#<ActiveRecord::Relation []>

它总是返回空白数组。我想做一个像这样的查询

  

Spree :: Order Load(4.1ms)SELECT DISTINCT&#34; spree_orders&#34;。* FROM   &#34; spree_orders&#34;在哪里&#34; spree_orders&#34;。&#34;州&#34; =&#39;完成&#39;和   &#34; spree_orders&#34;。&#34; payment_state&#34; =&#39; balance_due&#39;和   &#34; spree_orders&#34;。&#34; cod_pay&#34; =&#39; t&#39; AND&#34; spree_orders&#34;。&#34; shipment_state&#34;在   (&#39;准​​备好&#39;)ORDER BY&#34; spree_orders&#34;。&#34; completed_at&#34; DESC LIMIT 15 OFFSET   0

请指导如何进行此查询。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的查询均限于 shipment_state IN ('ready') ,但您的数据仅限shipment_state: "pending"。显然,结果必须是空的。

除此之外,你不需要DISTINCT,除非你的表中有完整的副本(不应该)。