ActiveRecord 不知道时区?

Kic*_*ski 6 ruby timezone ruby-on-rails utc

我有一个关于时区的问题

我已经完成了以下操作,当我执行以下查询时,我的活动记录仍然与我的时区不匹配

def stock_by_date(date)
   UserStock.where("date(created_at) = ? and user_id = ? ", date , current_user.id)
end
Run Code Online (Sandbox Code Playgroud)

我在我的身边做了以下事情application.rb

config.active_record.default_timezone = :utc
config.active_record.time_zone_aware_attributes = true
config.beginning_of_week = :sunday
config.active_record.time_zone_aware_types = [:datetime, :time]
Run Code Online (Sandbox Code Playgroud)

我在注册方法中添加了时区字段,它正确显示了当前时区,但是当我添加当前时区的股票时,它会显示在当前日时区的前一天或第二天,并且当我添加了我的库存,时间戳是我查看 Rails 控制台时的前一天

我的schema.rb

ActiveRecord::Schema.define(version: 2018_11_28_183416) do

  create_table "friendships", force: :cascade do |t|
    t.integer "user_id"
    t.integer "friend_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["friend_id"], name: "index_friendships_on_friend_id"
    t.index ["user_id"], name: "index_friendships_on_user_id"
  end

  create_table "notes", force: :cascade do |t|
    t.string "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.index ["user_id"], name: "index_notes_on_user_id"
  end

  create_table "stocks", force: :cascade do |t|
    t.string "ticker"
    t.string "name"
    t.decimal "last_price"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "user_stocks", force: :cascade do |t|
    t.integer "user_id"
    t.integer "stock_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["stock_id"], name: "index_user_stocks_on_stock_id"
    t.index ["user_id"], name: "index_user_stocks_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "first_name"
    t.string "last_name"
    t.string "time_zone", default: "UTC"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end
Run Code Online (Sandbox Code Playgroud)

例如在此输入图像描述 在此输入图像描述

我该如何解决这个问题?

注意:如果需要更多信息,请告诉我

注意:为了简洁起见,这是我的存储库

注意:我的配置根本没有显示出可靠的行为,我很困惑:/并且我今天也有同样的问题

- -更新 - - - - - - - - - -

在我的 Rails 控制台中

在此输入图像描述

xpl*_*oOn 1

使用此内容创建迁移以删除created_at的默认值,因此默认情况下不会在数据库级别填充它。

change_column_default(:user_stocks, :created_at, nil)
Run Code Online (Sandbox Code Playgroud)

之后,当您创建新的 UserStock 时,您需要指定created_at 值,并且您可以在其中指定created_at 和用户的日期,并注意时区。

UserStock.create(created_at: Time.now.in_time_zone(user.time_zone).to_time...)
Run Code Online (Sandbox Code Playgroud)

或者您可以将其添加到模型上的回调中,这样每次您创建 UserStock 时它都会自动更改值

class UserStock < ActiveRecord::Base
  before_create :set_created_at

  private
    def set_created_at
      self.created_at = time.now.in_time_zone(self.user.time_zone).to_time
    end
end
Run Code Online (Sandbox Code Playgroud)

因此,每次您创建一个值时,该值都是正确的。这样它就可以按预期工作。

如果您不需要仅用于该特定模型并将它们用于所有用户交互,则另一种选择是,您可以在应用程序控制器上创建一个 before 过滤器以动态设置时区,例如

class ApplicationController < ActionController::Base
  before_filter :set_time_zone

  def set_time_zone(&block)
    time_zone = current_user.try(:time_zone) || 'UTC'
    Time.use_zone(time_zone, &block)
  end
end
Run Code Online (Sandbox Code Playgroud)