【问题标题】:Spree: Return only Orders with Line Items in them大礼包:仅返回包含订单项的订单
【发布时间】:2018-06-16 15:37:15
【问题描述】:

我在 spree 应用程序中设置控制器时遇到问题,因此它只返回包含 line_items 的订单。

<Spree::Order id: 1057, number: "R498797188", item_total: #<BigDecimal:7f90c2acf4b8,'0.999E1',18(18)>, total: #<BigDecimal:7f90c2acf1e8,'0.1098E1',18(18)>, state: "payment", adjustment_total: #<BigDecimal:7f90c2ace770,'0.0',9(18)>, user_id: nil, completed_at: nil, bill_address_id: 1814, ship_address_id: 1815, payment_total: #<BigDecimal:7f90c2acc358,'0.0',9(18)>, shipping_method_id: nil, shipment_state: nil, payment_state: nil, email: nil, special_instructions: nil, created_at: "2015-09-10 22:31:23", updated_at: "2015-09-10 22:33:03", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: nil, shipment_total: #<BigDecimal:7f90c4b721e8,'0.199E1',18(18)>, additional_tax_total: #<BigDecimal:7f90c4b71fb8,'0.0',9(18)>, promo_total: #<BigDecimal:7f90c4b71e78,'0.0',9(18)>, channel: "spree", included_tax_total: #<BigDecimal:7f90c4b71798,'0.0',9(18)>, item_count: 1, approver_id: nil, approved_at: nil, confirmation_delivered: false, considered_risky: false>

Spree::Order 上有一条名为item_count 的记录需要> 0,但我不知道控制器中的语法需要是什么?

@orders = Spree::Order.all.where(:item_count > 0) #this returns the following error: comparison of Symbol with 0 failed

这看起来很简单,但任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: ruby-on-rails postgresql spree


    【解决方案1】:

    您可以使用 symbol 来检查绝对值,例如 where(item_count: 0),但不能用于比较。

    where 内部,当您使用符号时,实际上是在处理Hash 对象。 .where(item_count: 0)= .where({item_count: 0})
    并且要使用散列,您应该拥有像上面那样的键值对,而不是比较。 {:item_count &gt; 0} 是无效的哈希语法。

    为了在你的情况下进行比较,请使用字符串:

    @orders = Spree::Order.where('item_count > 0').all
    

    此外,如果需要,您应该在关系末尾使用.allall 返回范围对象,通常用于获取与之前的查询匹配的所有记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多