This project is archived and is in readonly mode.

#725 ✓wontfix
joahking

ActiveRecord UTC support on find with :conditions

Reported by joahking | July 30th, 2008 @ 10:47 AM | in 2.x

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')

Comments and changes to this ticket

  • Gabriel Medina

    Gabriel Medina October 31st, 2008 @ 09:38 AM

    • 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.

  • Pratik

    Pratik March 13th, 2009 @ 12:51 PM

    • Assigned user set to “Michael Koziarski”
  • Geoff Buesing

    Geoff Buesing March 14th, 2009 @ 03:47 PM

    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.

  • Pratik

    Pratik March 14th, 2009 @ 03:48 PM

    • State changed from “new” to “wontfix”
    • Assigned user changed from “Michael Koziarski” to “Geoff Buesing”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages