我看到很多类似的问题,但他们都倾向于关注 View 部分而不是数据库部分。假设您的客户需要将 created_at 字段保存在特定的本地时区,例如 EST。
默认情况下,rails 会将其保存在数据库中
Sun, 13 Oct 2013 20:35:47 +0000
我需要更改行为以便它保存此行为:
Sun, 13 Oct 2013 16:35:47 +0000
最佳答案
您可以在 config/application.rb
中配置 ActiveRecord 将考虑的时区。例如,您可以添加以下内容:
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'
来自official guides :
config.active_record.default_timezone
determines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :utc for Rails, although Active Record defaults to :local when used outside of Rails.
config.time_zone
sets the default time zone for the application and enables time zone awareness for Active Record.
关于ruby-on-rails - 如何更改 created_at 字段的 RAILS 默认时间戳格式(以存储在数据库中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350063/