Monthly Archive » May 2014

[Rails] ActiveRecord Time Zone – Not Automatically Set in Rails…

I bumped across this very interesting problem related to ActiveRecord time Zone…

I had some code snippet like this:

 query = "INSERT INTO link_logs VALUES(DEFAULT,'#{self.url}','#{self.affiliatized_url}','#{self.publisher_id}',DEFAULT,DEFAULT)"

Basically, a very simply Insert query to push url, affiliate_url , publisher_id and timestamps….

Problem: Timestamps were getting inserted in UTC Time Zone…


First, I checked Application Time Zone in config/application.rb and it was correctly set to Mumbai.

config.time_zone = 'Mumbai'

I then used ActiveRecord to insert records and Voila, it obeys the time zone set in application.rb

However, When done via execute query, It defaults to UTC TimeZone… Here’s what even the console says:

irb(main):001:0> Rails::application.config.time_zone

=> "Mumbai"

irb(main):002:0> Rails::application.config.active_record.default_timezone

=> nil

Turned out,  an open bug already exists for the same:

A workaround appears to be adding the following line to application.rb:

config.active_record.default_timezone = :local

Strongly would suggest from my experience, to avoid surprises, Active Record Default timezone must be manually set for any rails app