- Published on
Change default timezone for Active Record n Rails
In
application.rb,config.active_record.default_timezonedetermines whether to useTime.local(if set to:local) orTime.utc(if set to:utc) when pulling dates and times from the database. The default is:utc. https://guides.rubyonrails.org/configuring.html
If you want to change Rails timezone, but continues to have Active Record to save in the database in UTC, use
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
--------------------------------------------;
If you want to change Rails timezone AND have Active Record store times in this timezone, use
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
WARNING: you really should think twice, even thrice, before saving times in the database in a non-UTC format.
NOTES: Do not forget to restart your Rails server after modifying application.rb
-----------------------------------------------------;
Remember that config.active_record.default_timezone can take only two values
:local(converts to the timezone defined inconfig.time_zone):utc(converts to UTC)
------------------------------------------------------;
Here's how you can find all available timezones
rake time:zones:all