我需要在aplication.rb中使用参数
config.active_record.default_timezone = :utc
我的项目有一些需要它的GEM'(groupdate,SearchJoy)。
我遇到问题,其中created_at和updated_at列的保存时间错误(时间+2)
Rails控制台的示例摘录:
[2] pry(main)> >> System.create(name: 'TEST')
(0.5ms) BEGIN
SQL (1.0ms) INSERT INTO `systems` (`name`, `created_at`, `updated_at`) VALUES ('TEST', '2015-12-09 20:44:14', '2015-12-09 20:44:14')
(3.2ms) COMMIT
=> #<System:0xbc487a6c
id: 10,
name: "TEST",
created_at: Wed, 09 Dec 2015 18:44:14 BRST -02:00,
updated_at: Wed, 09 Dec 2015 18:44:14 BRST -02:00>
[3] pry(main)>
我的项目使用Rails 4.2.2和Mysql
aplication.rb:
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.active_record.default_timezone = :utc
# 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 = 'Central Time (US & Canada)'
config.time_zone = 'Brasilia'
Rails掌握任何想法?
答案 0 :(得分:0)
看起来它工作正常。
您的rails应用程序正在时区Brasilia
中运行并使用:utc
保存到数据库中,因此数据库中的值是正确的UTC值,当您从数据库中提取记录时自动转换为应用程序时区。
System.create(name: 'TEST') # this is in Brasilia, the data saved to the db should be offset from Brasilia to UTC
Time.use_zone { puts System.last.updated_at } # view the time in UTC
puts System.last.updated_at # view the time in Brasilia
我建议您更改config.time_zone = 'UTC'
,然后根据需要调整每个用户的time_zone(根据请求)
我写了一篇关于rails和时区的博文,你可能会觉得有用:http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/