Time calculations in Rails where statement

132 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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