如何在 Rails 中更改 Active Record 的默认时区? [英] How to change default timezone for Active Record in Rails?
问题描述
在我的 application.rb
中,我遇到了以下评论
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)'
正如您从上面看到的,我已将 config.time_zone
设为 EST 时间.但是,在数据库中创建记录时,看起来 datetime
仍然以 UTC 格式存储.
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
...并使 Active Record 自动转换到此区域...
...and make Active Record auto-convert to this zone...
我该怎么做,在哪里做?
How can I do that, and where?
此外,我也将在 heroku 上部署它,我希望该设置能够继续使用
Also, I'll be deploying this on heroku as well and i'd like the setting to carry over
推荐答案
将以下内容添加到 application.rb
作品
adding following to application.rb
works
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local # Or :utc
这篇关于如何在 Rails 中更改 Active Record 的默认时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!