0

I have an ActiveRecord model with the standard created_at, updated_at timestamps. I want to run a query to find all records created or updated in a time range.

It possible to do this in a single ActiveRecord query?

The following works great for a single clause (just querying on created_at, for example).

range = 2.weeks.ago .. 1.week.ago
=> Mon, 03 Jun 2013 14:49:54 UTC +00:00..Mon, 10 Jun 2013 14:49:54 UTC +00:00

Item.where(:created_at => range)
  Item Load (0.4ms)  SELECT `items`.* FROM `items` WHERE (`items`.`created_at` BETWEEN '2013-06-16 14:40:55' AND '2013-06-17 14:40:55')
=> [... results ...]

When I try to add the second clause ActiveRecord is producing an AND statement, which is not what I want:

Item.where(:created_at => range, :updated_at => range)
  SELECT `items`.* FROM `items`
    WHERE (`items`.`created_at` BETWEEN '2013-06-16 14:40:55' AND '2013-06-17 14:40:55')
    AND (`items`.`updated_at` BETWEEN '2013-06-16 14:40:55' AND '2013-06-17 14:40:55')

I could do the query with a single statement and add the results to a set to remove duplicates, but it really feels like something that should be a possible on once query.

And ideas?

4

3 回答 3

4

您通过搜索created_atAND使代码过于复杂updated_at。您只需要搜索,updated_at因为它也会在创建时设置。因此,您的问题的解决方案是:

range = 2.weeks.ago .. 1.week.ago
Item.where(:updated_at => range)
于 2013-06-17T18:56:59.003 回答
2

如果要将OR两个条件放在一起,则必须编写自己的语句:

Item.where("(created_at BETWEEN ? AND ?) or (updated_at BETWEEN ? and ?)", 2.weeks ago, 1.week ago, 2.weeks_ago, 1.week_ago)
于 2013-06-17T15:12:36.813 回答
0

看看gem "squeel"它肯定会帮助你。它真的很棒。也有关于此的 railscast:

https://github.com/ernie/squeel

http://railscasts.com/episodes/354-squeel?view=asciicast

于 2013-06-17T16:42:15.197 回答