Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails/Activerecord database field timezone

I have a database that is written to by a non-rails application and read from by a rails application. There is a datetime field in the database and the data stored in this field is stored in Eastern Time. In my rails app, I understand I can set the application's timezone in my environment.rb file by doing config.time_zone = 'Eastern Time (US & Canada)'. However, rails assumes the data stored in the database is stored in UTC and converts from config.time_zone to UTC as it pulls information in and out of the database.

Is there a way to tell rails the data in this particular field, or even all my datetime fields, is Eastern Time and not UTC?

like image 360
gotosleep Avatar asked Jul 14 '10 14:07

gotosleep


2 Answers

On Rails 3.0.5 in application.rb, this works:

config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
like image 86
Joe Goggins Avatar answered Nov 12 '22 18:11

Joe Goggins


config.active_record.default_timezone - Tells ActiveRecord to interpret database times as being in the specified time zone, and to use this timezone for working with the created_at & updated_at attributes.

like image 45
jmccartie Avatar answered Nov 12 '22 20:11

jmccartie