Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord UTC support on find with :conditions

#725

ActiveRecord caveat to care about regarding UTC conversions:

# some initializations
>> time_str = "2008-07-30T09:44:28+02:00"
>> time = Time.parse time_str

if used then like this:

>> XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", time_str])

you see this query in the log:

SELECT * FROM "XXX" WHERE (created_at >= '2008-07-30T09:44:28+02:00')

same happens with:

>> XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", time])

WORKAROUND:

>> XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", time.utc])

then you get this perfectly in the log:

SELECT * FROM "messages" WHERE (created_at >= '2008-07-30 07:44:28')

Reported by joahking · July 30th, 2008 @ 10:47 AM

State: wontfix
Milestone: 2.x
Assigned to: Geoff Buesing Geoff Buesing
Importance: none

Activity

  1. Gabriel Medina
    Gabriel Medina
    • Tag changed from :conditions, activerecord, find, utc to :conditions, activerecord, connection, date, datetime, find, quote, quote_bound_value, time, utc

    Hi, can't reproduce error ...

    SQLite3

    Loading development environment (Rails 2.2.0) >> time_str = "2008-07-30T09:44:28+02:00" => "2008-07-30T09:44:28+02:00" >> time = Time.parse time_str => Wed Jul 30 01:44:28 -0600 2008 >> Post.connection.quote(time) => "'2008-07-30 01:44:28'" >>

    MySQL

    Loading development environment (Rails 2.1.1) >> time_str = "2008-07-30T09:44:28+02:00" => "2008-07-30T09:44:28+02:00" >> time = Time.parse time_str => Wed Jul 30 01:44:28 -0600 2008 >> Post.connection.quote(time) => "'2008-07-30 01:44:28'" >>

    Post is a model I made in both MySQL and SQLite3.

    This value is handled by quote_bound_value in activerecord/lib/active_record/base.rb as follows ...

    def quote_bound_value(value) #:nodoc:

    if value.respond_to?(:map) && !value.acts_like?(:string)
      if value.respond_to?(:empty?) && value.empty?
        connection.quote(nil)
      else
        value.map { |v| connection.quote(v) }.join(',')
      end
    else
      connection.quote(value)
    end
    
    

    end

    This is equivalent to Post.connection.quote(something).

    Can anybody else confirm or deny this?

    Thanks,

    Gabriel Medina.

    October 31st, 2008 @ 09:38 AM

  2. Pratik
    Pratik
    • Assigned user set to Michael Koziarski

    March 13th, 2009 @ 12:51 PM

  3. Geoff Buesing
    Geoff Buesing

    ActiveRecord conditions don't do automatic UTC conversions on Time objects passed in as arguments, so the fact that this query:

    
    XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", Time.now])
    

    is not equivalent to this query:

    
    XXX.find_all_by_YYY('yyy', :conditions => ["created_at >= ?", Time.now.utc])
    

    is expected behavior.

    If ActiveRecord did automatically coerce Time objects in conditions to ActiveRecord::Base.default_timezone, that could potentially be handy behavior, but it would likely break apps that wouldn't be expecting this.

    March 14th, 2009 @ 03:47 PM

  4. Pratik
    Pratik
    • State changed from new to wontfix
    • Assigned user changed from Michael Koziarski to Geoff Buesing

    March 14th, 2009 @ 03:48 PM