views:

164

answers:

3

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!

A: 

Easiest way to debug is to look in your rails log to see exactly what SQL statement is being generated by Rails.

Then post the sql if still having problems.

In the meantime, my guess is that something is not set to UTC. Where something is the set {operating system, rails environment, dbms}

Added: Also, why are you comparing "Date" (in the dbms) with "Time" values (from your statement)? Better to have the type classes match explicitly. I use the standard of a new day has time component 00:00:00. That way you can compare with the db without needing the date function in your SQL.

Larry K
Hi Larry K. What I found yesterday was that the second event, the one that I wanted to show DID show for most of the day and appeared to no longer be current after UTC time passed. Starting at 8pm EST for instance, the event was no longer current, but at 5pm EST it was which does make me think it's a time zone issue. Here's the output from the development log for the query on my local machine:SELECT * FROM `events` WHERE ((Date(start_date) <= '2010-02-17 10:14:10' AND Date(end_date) >= '2010-02-17 10:14:10') AND (events.deleted_at IS NULL OR events.deleted_at > '2010-02-17 15:14:50'))
aressidi
I've updated the date_current named_scope to look like this now:named_scope :date_current, :conditions => ["Date(start_date) <= ? AND Date(end_date) >= ?", Time.now.in_time_zone("UTC").to_date.to_s, Time.now.in_time_zone("UTC").to_date.to_sThis produces the following in the log:SELECT * FROM `events` WHERE ((Date(start_date) <= '2010-02-17' AND Date(end_date) >= '2010-02-17') AND (events.deleted_at IS NULL OR events.deleted_at > '2010-02-17 15:22:44')) Perhaps the time zone needs to be +5 hours instead of -5 hours for this to work...
aressidi
A: 

I struggled with the same issue. The records are stored in UTC, but the utc value is not inserted into the query. I solved it, like you, by converting it to utc manually, like this time.utc.

def find_production_since(start_time)
    find(:all, :conditions => ["time > ?", start_time.utc])
end
Ole Morten Amundsen
A: 

Rather than using Time.now (and then laboriously converting to a date as per your second comment) you could just use Date.today, which will insert '2010-04-20' into your SQL, and should compare against Date(start_date) without worrying about times.

Bruce