1

我在这篇 SO 帖子中读过,你可以像这样any_of结合between

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'

我在这里想念什么?

4

1 回答 1

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
)
于 2014-06-19T09:33:07.110 回答