Time.current的文档说:
在设置activesupport/lib/active_support/core_ext/time/calculations.rb,或config.time_zone时返回Time.zone.now,否则只返回Time.now.File .File行36 def current ::Time.zone ::Time.zone.now:::Time.now end
但是,Time.zone 何时不在Rails中设置?
# Set Time.zone default to the specified zone and make Active Record
在结果中,方法Time.zone.now总是返回相同的值,而Time.now则返回正确的系统时间。
这里有一个例子:
banner.rb
scope :active, where("'#{Time.zone.now}' between start_date and end_date")
此范围的日志:
SELECT `banners`.* FROM `banners` WHERE ('2013-03-06 08:06:46 -0300' between start_date and end_date) AND ((`banners`.`store
在具有activerecord的Rails 5中,只有在当前为零的情况下更新属性的首选方法是什么。
car = Car.first
car.connected_at = Time.zone.now
car.save
OR
car = Car.first
car.update!(connected_at: Time.zone.now)
it should update only if car.connected_at is nil
顾名思义,我不知道发生了什么事。我的系统时区是CST。在Rails的application.rb文件中,我将time_zone设置为CST (-6):
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Central Ti
我有约会时间文件order_confirmed_at和completion_confirmed_at
class CoolModel < ActiveRecord::Base
attr_accessible :order_confirmed, completion_confirmed
def order_confirmed
order_confirmed_at.present?
end
def order_confirmed=(state)
if state and order_confirmed_at.blank?
self.order_
重现步骤
大家好。我目前正在学习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 u