0

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?

Jaro
  • 860
  • 9
  • 21
  • What is the datatype of your `life_time`? – Salil Oct 22 '12 at 10:11
  • It's `int` but in view `published_time + life_time` works fine and add days. In where statemet life_time.days doesn't work. – Jaro Oct 22 '12 at 10:18
  • are you sure you are using the correct magnitudes? I believe when rails compares two date types that are in different formats, it will convert the dates to seconds since epoch – n_i_c_k Oct 22 '12 at 10:34

1 Answers1

1

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

Salil
  • 46,566
  • 21
  • 122
  • 156