我的 rails 4.1.7.1 应用程序运行良好。至少,我想要的方式。但是,在升级到 4.2(使用官方升级指南)后,我遇到了一些问题。
第一的。使用 DateTime.parse 会引发错误:'undefined method `getlocal'。代码是:
# Add the schedule
schedule_in_db = Schedule
.where(
:event_id => event_in_db.id,
:ext_id => schedule['id']
)
.first_or_create!(
:start_time => DateTime.parse(schedule['start_time']), # example value: 2014-12-18T14:00:00.000+00:00"
:end_time => DateTime.parse(schedule['end_time']),
:type_id => type_in_db.id
)
为了解决这个问题,我改用了 Time.parse 或 Time.zone.parse。这修复了错误。然而,一个新的问题出现了。我的 start_time 和 end_time 变量提前一小时得到填充。这样 08:15 就变成了 09:15。
这是我的 app.rb 配置:
config.time_zone = 'Stockholm' #uncommented and set to proper zone
config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false
无论我尝试什么组合,我的事件仍然提前一小时。
有趣的是,在创建计划之后,这将返回零计划:
Schedule.where start_time: schedule['start_time']
但是,这将返回正确的行:
Schedule.where start_time: Time.parse(schedule['start_time'])
我想我的问题是:为什么 rails 4.2 在日期和时间方面与具有相同代码库的 4.1.7.1 表现如此不同?什么是补救措施?