Rails 3在多个日期范围内查询

时间:2012-04-06 10:29:10

标签: ruby-on-rails-3 activerecord active-relation

假设我们有一些日期范围,例如:

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)这样的表示法来进行此查询?

构建此查询的正确方式是什么?

谢谢

3 个答案:

答案 0 :(得分:7)

paren有点咄咄逼人,但准备潜入arel兔子洞

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))
         ]
table = Post.arel_table
query = ranges.inject(table) do |sum, range|
  condition = table[:created_at].in(range)
  sum.class == Arel::Table ? condition : sum.or(condition)
end

然后,query.to_sql应该等于

(((("sessions"."created_at" BETWEEN '2011-06-05 12:23:32.442238' AND '2011-10-05 12:23:32.442575' OR "sessions"."created_at" BETWEEN '2011-11-05 12:23:32.442772' AND '2011-12-05 12:23:32.442926') OR "sessions"."created_at" BETWEEN '2012-01-05 12:23:32.443112' AND '2012-02-05 12:23:32.443266') OR "sessions"."created_at" BETWEEN '2012-03-05 12:23:32.443449' AND '2012-04-05 12:23:32.443598') OR "sessions"."created_at" BETWEEN '2012-05-05 12:23:32.443783' AND '2012-05-21 12:23:32.443938')

你应该能够做Post.where(查询)

修改


您还可以执行以下操作:

range_conditions = ranges.map{|r| table[:created_at].in(r)}
query = range_conditions.inject(range_conditions.shift, &:or)

让它更加简洁

答案 1 :(得分:1)

我建议你尝试纯字符串形式:

# e.g. querying those in (12.months.ago .. 8.months.ago) or in (7.months.ago .. 6.months.ago)
Post.where("(created_at <= #{12.months.ago} AND created_at >= #{8.months.ago} ) OR " + 
  "(created_at <= #{7.months.ago} AND created_at >= #{6.months.ago} )" )

答案 2 :(得分:-3)

在你的情况下,我建议使用mysql IN子句

Model.where('created_at IN (?)', ranges)