Ruby - Mongoid - 无法查询日期范围,错误:键必须是字符串或符号

时间:2012-01-05 23:57:28

标签: ruby mongoid where-clause

以下查询不起作用,并且它不断发出奇怪的错误: 键必须是字符串或符号

Session.where(:created_at.gte => (Date.today - 30), :created_at.lte => (Date.today).first

以下是我检查时查询的内容:

:::::::::::::::::: # selector: {#=>Wed, 04 Jan 2012, #=>Thu, 05 Jan 2012},
options: {},
class: Session,
embedded: false>
:::::::::::::::::: # selector: {#=>Wed, 04 Jan 2012, #=>Thu, 05 Jan 2012},
options: {},
class: Session,
embedded: false>
:::::::::::::::::: # selector: {#=>Wed, 04 Jan 2012, #=>Thu, 05 Jan 2012},
options: {},
class: Session,
embedded: false>

关于我可能做错了什么或做mongoid的任何想法都有错误?

1 个答案:

答案 0 :(得分:0)

我与宝石的开发者交谈,他说它在rails 3.1 +中得到了支持 此外,我们通过使用不同的语法(类似于javascript)来完成这项工作。

scope :between, ->(from, to){ where(:created_at => {'$gte' =>  Time.parse(from.to_s)}).and(:created_at => {'$lte' => Time.parse((to + 1.day).to_s) - 1.second }) }

事实证明这在rails 3.0中得到了支持