How to change default timezone for Active Record i

2019-01-01 01:37发布

In my application.rb I came across the following comment

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
 config.time_zone = 'Eastern Time (US & Canada)'

As you see from above, I've made config.time_zone to EST time. However, still when records are created in the DB, it looks like datetime is being stored in UTC format.

In the above comment, they say

...and make Active Record auto-convert to this zone...

How can I do that, and where?

Also, I'll be deploying this on heroku as well and i'd like the setting to carry over

9条回答
残风、尘缘若梦
2楼-- · 2019-01-01 02:29

I had to add this block to my environment.rb file and all was well :)

Rails.application.configure do
    config.time_zone = "Pacific Time (US & Canada)"
    config.active_record.default_timezone = :local
end
  • I added it before the line Rails.application.initialize!
查看更多
弹指情弦暗扣
3楼-- · 2019-01-01 02:29

for Chinese user, just add two lines below to you config/application.rb :

config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
查看更多
只靠听说
4楼-- · 2019-01-01 02:40

In my case (Rails 5), I ended up adding these 2 lines in my app/config/environments/development.rb

config.time_zone = "Melbourne"
config.active_record.default_timezone = :local

That's it! And to make sure that Melbourne was read correctly, I ran the command in my terminal:

bundle exec rake time:zones:all

and Melbourne was listing in the timezone I'm in!

查看更多
登录 后发表回答