【问题标题】:Rails - Postgresql updating datetime leaves me with correct date but time is 00:00:00Rails - Postgresql 更新日期时间给我留下正确的日期,但时间是 00:00:00
【发布时间】:2013-10-31 10:59:16
【问题描述】:

我一直在尝试使用当前日期和时间更新日期时间字段。

current_user.update_attributes!(:watched_post_its_at => Time.now)

但它给我留下了2013-10-31 00:00:00

如果我进入控制台并像这样写

User.find(8).update_attributes!(:watched_post_its_at => Time.now)

它给我留下了正确的时间:2013-10-31 10:57:56

我在这里错过了什么?

在 Rails 4 上使用 PostgreSQL。

【问题讨论】:

    标签: ruby-on-rails ruby datetime time rails-postgresql


    【解决方案1】:

    我被困在同一个问题上。我发现我需要为 Postgres 明确引入格式。在我的情况下,我使用这个:

    Time.at(time_param).to_formatted_s(:db)
    

    在你的问题中你需要使用 .to_formatted_s(:db),像这样

    User.find(8).update_attributes!(:watched_post_its_at => Time.now.to_formatted_s(:db))
    

    我签入了我的代码,它可以工作

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      相关资源
      最近更新 更多