Setup
Rails' where
method can take a range in a hash to generate a query that will search for a value that is within the range. For example:
User.where(cash_money: 10..1000)
#=> SELECT `users`.* FROM `users` WHERE (`users`.`cash_money` BETWEEN 10 AND 1000)
This can also be used with timestamps like
User.where(last_deposit: 10.days.ago..1000.days.ago)
#=> SELECT `users`.* FROM `users` WHERE (`users`.`last_deposit` BETWEEN '2014-05-19 14:42:36' AND '2011-09-02 14:42:36')
I've found that you can do a simple less than or greater than with numbers using the hash syntax like this
User.where(cash_money: 10..Float::INFINITY)
#=> SELECT `users`.* FROM `users` WHERE (`users`.`cash_money` >= 10)
and the same can be done with -Float::INFINITY
for less than queries.
Question
Is there a way to do this with timestamps so I can get a query like the following?
SELECT `users`.* FROM `users` WHERE (`users`.`last_deposit` >= '2014-05-19 14:42:36')
I cannot use Float::INFINITY
or Date::Infinity
with a range as they both error with ArgumentError: bad value for range
.
Current Simple Solution
User.where('`users`.`last_deposit` >= ?', 10.days.ago)
will generate the same SQL but if this can be done with objects other than strings, I'd like to do so.
Potential (Meh) Answer
This is kind of lousy but it could be done with ranges using Time.at(0)
and Time.at(Float::MAX)
. I have a feeling these could result in equally lousy SQL queries though.