The usual approach with ActiveRecord is to treat datetime columns as
time-zone-less -- in other words, regardless of whether postgres has
an internal idea of time zone, you interpret the values of those
columns in whatever time zone makes sense.

To make that less of a pain, many applications set ActiveRecord to
persist all timestamps as UTC -- this is just a configuration setting
you can put into your environment.rb:

    ActiveRecord::Base.default_timezone = :utc

Then, whenever you're doing time-based queries, you need to remember
to cast the time in question to UTC. One could make the argument that
ActiveRecord should do this for you, or that it shouldn't, but either
way it doesn't, so it's just something you have to remember to do in
your code.

Naturally, operations that assume that your idea of a time zone and
the database's are the same -- such as performing a query with NOW()
-- are going to cause you problems; but since you're probably building
all your queries in Ruby anyway, it's easy enough to avoid those
situations.

Mat

On Tue, Apr 20, 2010 at 13:39, Matthew Moore <m3mo...@gmail.com> wrote:
> I have a model that I'm trying to do a find along with conditions that
> rely on Time.now.
>
> When I run 2 simple queries, one with created_at <= Time.now, and one
> without, I expect to find the exact same record (because no new posts
> have been created in between these calls).
>
> But I get different results --
>
> Here's my output:
>>> Time.now
> => Tue Apr 20 10:38:01 -0700 2010
>>> Time.now.utc # For verification purposes
> => Tue Apr 20 17:38:05 UTC 2010
>>> Post.find(:first, :conditions => ['created_at <= ?', Time.now], :order => 
>>> 'created_at DESC')
> => #<Post id: 7835, created_at: "2010-04-20 05:46:03", updated_at:
> "2010-04-20 05:46:03">
>>> Post.find(:first, :order => 'created_at DESC')
> => #<Post id: 7866, created_at: "2010-04-20 17:17:26", updated_at:
> "2010-04-20 17:17:26">
>
>
> Is this because the Timezone of the server is set wrong?  Something
> funky with Postgres?
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Heroku" group.
> To post to this group, send email to her...@googlegroups.com.
> To unsubscribe from this group, send email to 
> heroku+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/heroku?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Heroku" group.
To post to this group, send email to her...@googlegroups.com.
To unsubscribe from this group, send email to 
heroku+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/heroku?hl=en.

Reply via email to