我正在尝试在我的模型上保存日期时间字段。我保存正确,但是当我获取记录时,它的值为零。我正在使用带有postgresql的Rails 4.2.4。例如:
2.2.2 :006 > Post.create(data_agendada: DateTime.now)
(0.4ms) BEGIN
SQL (0.7ms) INSERT INTO "posts" ("data_agendada", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["data_agendada", "2016-01-31 17:45:42.895390"], ["created_at", "2016-01-31 17:45:42.897885"], ["updated_at", "2016-01-31 17:45:42.897885"]]
Sun, 31 Jan 2016 17:45:42 BRST -02:00
(34.5ms) COMMIT
Post id: 71, cod: nil, data: nil, legenda: nil, created_at: "2016-01-31 19:45:42", updated_at: "2016-01-31 19:45:42", tags: nil, facebook: nil, twitter: nil, instagram: nil, app: nil, materia_id: nil, shares_count: nil, fonte_id: nil, user_id: nil, likes_count: nil, data_agendada: "2016-01-31 19:45:42"
2.2.2 :007 > Post.last
Post id: 71, cod: nil, data: nil, legenda: nil, created_at: nil, updated_at: nil, tags: nil, facebook: nil, twitter: nil, instagram: nil, app: nil, materia_id: nil, shares_count: nil, fonte_id: nil, user_id: nil, likes_count: nil, data_agendada: nil
有谁知道这是什么问题?谢谢!
答案 0 :(得分:0)
如果您的默认时区设置不正确,可能会在datetime字段中发生这种情况。有关详细信息,请查看此issue。
您的application.rb
应该有rake time:zones:all
的时区(或根本没有)。
config.time_zone = "Brasilia"