假设我们有一些日期范围,例如:
ranges = [
[(12.months.ago)..(8.months.ago)],
[(7.months.ago)..(6.months.ago)],
[(5.months.ago)..(4.months.ago)],
[(3.months.ago)..(2.months.ago)],
[(1.month.ago)..(15.days.ago)]
]
和一个Post
具有:created_at
属性的模型。
我想查找created_at
值在此范围内的帖子,因此目标是创建如下查询:
SELECT * FROM posts WHERE created_at
BETWEEN '2011-04-06' AND '2011-08-06' OR
BETWEEN '2011-09-06' AND '2011-10-06' OR
BETWEEN '2011-11-06' AND '2011-12-06' OR
BETWEEN '2012-01-06' AND '2012-02-06' OR
BETWEEN '2012-02-06' AND '2012-03-23';
如果您只有一个这样的范围:
range = (12.months.ago)..(8.months.ago)
我们可以做这个查询:
Post.where(:created_at => range)
和查询应该是:
SELECT * FROM posts WHERE created_at
BETWEEN '2011-04-06' AND '2011-08-06';
有没有办法使用这样的符号进行此查询Post.where(:created_at => range)
?
构建此查询的正确方法是什么?
谢谢