Time calculations in Rails where statement
-
13-07-2021 - |
Question
I'm trying to make some time calculations in where statement. I would like to select offers which life time has already past.
select_offers = Offer.where { (published_time + life_time) < DateTime.now }
It doesn't throw any error but it also doesn't return objects. I'm using squeel gem. What is the best approach to this?
Solution
Assuming life_time
is the number of days for which offer is valid.
select_offers = Offer.where(["DATE_ADD(published_time, INTERVAL life_time DAY) < ? " , DateTime.now ]
Ref:- DATE_ADD
function of mysql
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow