Rails: Finally Dealing With Timestamps Correctly

Can’t help to share my joy. If you did this (see below) prior to current Rails Edge version when your time zone in environment.rb is set to anything but “UTF-8”, you did it wrong.

class Model < ActiveRecord::Base
   named_scope ..., :conditions => { :date_time_field => }

Why? Because the will be taken in your server local time zone (set in environment.rb) and compared against the UTC saved in the database. To do it right, you needed this:


The same was applicable to finders and other stuff taking dates as arguments. Not very nice. I personally wrote patches to fix this in my projects.

Finally, in the latest Rails Edge it was fixed! No more kludge. Taken from the official post:

What time is it!?

Geoff Buesing provided a useful fix for Time, specifically when used in conjunction with ActiveRecord. Now you can save and search ActiveRecord objects using whichever local time zone you like, regardless of what your default time zone is configured for and everything now will just work. You no longer need to be concerned about converting your user’s local time into your default application time or vice versa.