如何在Mongoid 4中结合any_of和?

时间:2014-06-19 08:22:52

标签: ruby mongoid mongoid4

我已在this SO post中读到这一点,您可以将any_ofbetween合并为:

webshop = Webshop.first

webshop.orders.any_of(
  webshop.orders.between(:datetime_pending, [Time.zone.now-7.days, Time.zone.now]).selector, # An error is raised here.
  webshop.orders.between(:datetime,         [Time.zone.now-7.days, Time.zone.now]).selector
)

但是当使用Mongoid 4尝试此查询时,我收到错误:

ArgumentError: wrong number of arguments (2 for 0..1).
/Users/christoffer/project/vendor/gems/ruby/2.0.0/gems/origin-1.1.0/lib/origin/selectable.rb:63:in `between'
/Users/christoffer/project/vendor/gems/ruby/2.0.0/bundler/gems/mongoid-b91705b0ded8/lib/mongoid/relations/referenced/many.rb:413:in `block in method_missing'
/Users/christoffer/project/vendor/gems/ruby/2.0.0/bundler/gems/mongoid-b91705b0ded8/lib/mongoid/scopable.rb:238:in `with_scope'
/Users/christoffer/project/vendor/gems/ruby/2.0.0/bundler/gems/mongoid-b91705b0ded8/lib/mongoid/relations/referenced/many.rb:412:in `method_missing'

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

根据文档Queryable#between采用哈希(具有范围值的键)。

您是否尝试过传递而不是数组?

webshop = Webshop.first
now = Time.zone.now
seven_days = now - 7.days
webshop_orders = webshop.orders

webshop_orders.any_of(
  webshop_orders.between(datetime_pending: seven_days..now).selector,
  webshop_orders.between(datetime:         seven_days..now).selector
)