最近想换updated_at
一个帖子的专栏,但没那么简单!
尽管如此,服务器时间是 12:06,当我使用控制台 (irb) 时是 12:06
还试过:
irb(main):001:0> Time.zone.to_s
=> "(GMT+03:00) Moscow"
当我这样做时:
post.update!(updated_at: DateTime.now)
=> true
如果我检索记录,结果是“2018-10-29 09:06:47”
任何帮助表示赞赏!
最佳答案
尝试使用 DateTime.current
- 如果您的应用程序在一个中运行,这将获得分区时间。
从文档:
Returns Time.zone.now.to_datetime when Time.zone or config.time_zone are set, otherwise returns Time.now.to_datetime.
基本上,
Time.zone
应该反射(reflect)DateTime.current
如果您使用时区,则 Time.now
和 DateTime.now
将忽略任何区域。该方法的来源在这里是不言自明的:def current
::Time.zone ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime
end
希望有所帮助 - 如果您有任何问题,请告诉我。
关于ruby-on-rails - Rails 从哪里获取它们的 DateTime.now?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53042379/