RailsPostgredatetime储蓄为零 - Rails Postgre datetime saving as nil

- 此内容更新于:2016-02-01
主题:

我试图拯救一个datetime字段在我的模型。我保存错的话,但是当我获取记录,它的值是零。我使用Rails4.2.4postgresql。例如:有人知道是什么问题?谢谢!

原文:

I'm trying to save a datetime field on my model. I saves correctly, but when I fetch the record, its value is nil. I'm using Rails 4.2.4 with postgresql. Example:

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

Does anyone know what's the problem? Thanks!

网友:你可以这样做:1)测试=。新(data_agendada:DateTime.now);2)test.valid吗?test.errors;(3)

(原文:Can you do this: 1) test = Post.new(data_agendada: DateTime.now); 2) test.valid?; (3) test.errors)

网友:或者说的输出

(原文:or rather the output of Post.create!(data_agendada: DateTime.now))

网友:2.2.2输出::004>test.valid吗?2.2.2=>true:005>测试。错误=>#,@messages={}>

(原文:Here's the output: 2.2.2 :004 > test.valid? => true 2.2.2 :005 > test.errors => #<ActiveModel::Errors:0x000000054a27e8 @base=#<Post id: nil, 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: "2016-01-31 22:17:17">, @messages={}>)

网友:和Post.create!:对于Post.create!:=>#看起来像它正确保存,但当我获取记录,存储的值是null。我也试图执行sqlpostgre控制台,但是我也得到了相同的结果。保存日期就可以了。它只发生datetime

(原文:And for Post.create!: and for Post.create!: => #<Post id: 75, cod: nil, data: nil, legenda: nil, created_at: "2016-01-31 22:18:42", updated_at: "2016-01-31 22:18: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 22:18:42"> Looks like its saving correctly, but when I fetch the record, the value stored is null. I also tried to execute the sql on postgre console, but I also got the same result. Saving date is ok. It only happens for datetime)

解决方案:
像这样可以发生在datetime字段如果默认时区设置不正确。更多信息查看这个问题。你应该有一个时区(或者根本没有)。
原文:

Looks like this can happen with datetime fields if your default timezone is set incorrectly. Check out this issue for more info.

Your application.rb should have a time zone from rake time:zones:all (or none at all).

config.time_zone = "Brasilia"
网友:猜时区是正确配置:配置。time_zone=config.active_record巴西利亚。default_timezone='巴西利亚'

(原文:guess time zone is configured properly: config.time_zone = 'Brasilia' config.active_record.default_timezone = 'Brasilia')

网友:也许试着删除它们,重新启动您的控制台然后看看它的工作原理。如果是这样,那么问题将相关的部分。否则我将寻找其他可能的问题

(原文:Maybe try removing them, restarting your console and seeing if it works then. If it does, then the problem would be related to that part. Otherwise I'll look for other possible problems)

网友:已经重新启动控制台,电脑和其他的一切。也试着创建一个测试模型只有一个datetime字段…它还ocurrs同样的问题,所以想在postgre可能有些问题,我想。

(原文:Already restarted console, computer and everything else. Also tried create a test model with only one datetime field... It also ocurrs the same problem, so guess may be some problem at postgre, I guess.)

网友:你删除了时区配置,输入,然后railsc?

(原文:Did you remove the timezone config, type exit in rails c, and then rails c, Post.last?)

网友:谢谢!那工作!但是我的配置是错误的吗?

(原文:Thanks! That works! But what was wrong on my configuration?)