【问题标题】:Time.zone.now not working in Rails database (Learn enough tutorial)Time.zone.now 在 Rails 数据库中不起作用(学习足够的教程)
【发布时间】:2017-11-27 14:15:56
【问题描述】:

重现步骤

大家好。我目前正在阅读 Ruby on Rails 教程的第 11 章。目前我正试图让 Time.zone.now 工作。

在控制台上,当我输入 Time.zone.now 时,一切正常

2.4.0 :007 > Time.zone.now
=> Fri, 23 Jun 2017 22:34:33 UTC +00:00


class AccountActivationsController < ApplicationController

  def edit
    user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      # (1) time set to a variable
      time = Time.zone.now 
      user.update_attribute(:activated,    true)
      # (2) This time variable results to year 2000
      user.update_attribute(:activated_at, time) 
      log_in user
      # (3) This time variable results to year 2017
      flash[:success] = "Account activated! #{time}"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end
  end
end

哪里坏了

下面是我在rails控制台输入User.last时的输出

 => #<User id: 110, name: "foo", email: "foo@email.com", created_at: "2017-06-23 22:28:09", updated_at: "2017-06-23 22:28:21",...., activated: true, activated_at: "2000-01-01 22:28:21"> 
2.4.0 :009 >

如您所见,activated_at 显示正确的时间,但不显示正确的年、日和月。 然而,相同的时间变量被用作上述注释 (3) 中提到的闪存消息。

result form: flash[:success] = "Account activated! #{time}" (comment 2)

有什么想法吗?

我的尝试

我所能尝试的只是 config/application.rb 中以下配置的混合

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module SampleApp
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    config.active_record.time_zone_aware_types = [:datetime, :time]
    # config.active_record.time_zone_aware_types = [:datetime]
    config.active_record.default_timezone = :local

  end
end

【问题讨论】:

  • 只是猜测。它会因为您没有为 Rails 设置任何时区而发生吗?你能试着像config.time_zone = 'Hawaii'一样设置它applictaion.rb吗?如果这不起作用,请尝试将其设置为实际操作,例如:Time.zone = 'Hawaii' ; time = Time.zone.now.
  • 你为什么要设置time_zone_aware_typesdefault_timezone
  • @Sajan 我不是所有这些:config.active_record.time_zone_aware_types = [:datetime, :time] config.active_record.time_zone_aware_types = [:datetime] config.active_record.default_timezone = :local I应该试试:[config.time_zone = 'Hawaii'] ?
  • @jdgray 是的,你是对的,这是我的错误。但我尝试只设置 config.active_record.* 选项之一,但没有任何效果。这很奇怪,因为我的用户“created_at”显示了正确的日期,但“activated_at”却没有。

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


【解决方案1】:

这听起来像是数据库中列类型的问题。我不确定您使用的是什么数据库,但是当数据库列类型为 time 而不是 datetime 时,我已经看到过这种情况。

检查您的db/schema.rbdb/structure.sql 并确认activated_at 列的类型。

在幕后 postgres 和 mysql 将时间戳值存储为 2000-01-01 午夜之前或之后的秒数。

【讨论】:

  • 完全正确...我写的是数据时间而不是日期时间。感谢您的意见!
  • 太棒了!很高兴它有帮助。如果您不介意单击答案旁边的复选标记,这将帮助有类似问题的其他用户找到适合您的方法。谢谢!
猜你喜欢
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
相关资源
最近更新 更多