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?