date comparisons in Rails

Posted by aressidi on Stack Overflow See other posts from Stack Overflow or by aressidi
Published on 2010-02-17T04:54:09Z Indexed on 2010/04/18 19:03 UTC
Read the original article Hit count: 321

Filed under:
|
|

Hi there, I'm having trouble with a date comparison in a named scope. I'm trying to determine if an event is current based on its start and end date. Here's the named scope I'm using which kind of works, though not for events that have the same start and end date.

named_scope :date_current, :conditions => ["Date(start_date) <= ? AND Date(end_date) >=  ?", Time.now, Time.now]

This returns the following record, though it should return two records, not one...

>> Event.date_current
=> [#<Event id: 2161, start_date: "2010-02-15 00:00:00", end_date: "2010-02-21 00:00:00", ...]

What it's not returning is this as well

>> Event.find(:last)
=> #<Event id: 2671, start_date: "2010-02-16 00:00:00", end_date: "2010-02-16 00:00:00", ...>

The server time seems to be in UTC and I presume that the entries are being stored in the DB in UTC. Any ideas as to what I'm doing wrong or what to try?

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about sql