Take the 2-minute tour ×

I have a model with an expiration datetime field that I've been comparing to Time.now in finds.

Now my client wants the model to persist in the view until midnight of it's expiration day.

So, I think what I need to do is be able to call the end_of_day method on the field as part of the find.

Something like…

Model.where('expiration.end_of_day < ?', Time.now)

That certainly doesn't work and maybe I'm just going about this all wrong.

This is on Rails 3.2 using Postgres (9.1 I think. It's on Heroku).

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

I'm assuming that your expiration column is a timestamp rather than a date, which is where the problem stems from. I think something like the following should work for you:

Model.where('expiration > ?', Time.now.beginning_of_day)  # All records which expire today or later
share|improve this answer
Yes it's a timestamp. Handling this with beginning_of_day on Time.now rather than the way I was looking at it is perfect. –  kjs3 Jun 1 '12 at 14:49
This actually needed to be Model.where('expiration > ?', Time.zone.now.beginning_of_day) for the comparison to work. Time.now's class is Time while ActiveRecord returns the class ActiveSupport::TimeWithZone from the expiration field. –  kjs3 Jun 1 '12 at 16:47
Ah, yeah, makes sense. Glad you got it working. –  Wizard of Ogz Jun 1 '12 at 19:09
add comment

In pure postgresql it would be this:

expiration < current_date + 1

select current_date;
(1 row)

select '2012-06-01 23:59:59' < current_date + 1;
(1 row)
share|improve this answer
add comment

Your Answer


