Ruby on rails 当它在一个范围内使用时,会出现非常奇怪的时间问题
my Rails应用程序中有一个页面,其中应显示tours,以便start_date字段等于明天的日期(以GMT+00为单位) 我在application.rb中使用默认时区Ruby on rails 当它在一个范围内使用时,会出现非常奇怪的时间问题,ruby-on-rails,date,heroku,Ruby On Rails,Date,Heroku,my Rails应用程序中有一个页面,其中应显示tours,以便start_date字段等于明天的日期(以GMT+00为单位) 我在application.rb中使用默认时区 # Set Time.zone default to the specified zone and make Active Record ... # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # c
# Set Time.zone default to the specified zone and make Active Record ...
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
然而,这其中存在一个问题。我看到的不是明天开始的巡演日期,而是今天甚至昨天的日期。
我在页面上添加了以下信息:
Time.zone (GMT+00:00) UTC
Date.today_local_zone 2013-11-20
Date.today 2013-11-20
Time.now 2013-11-20 00:48:21 +0000
我的控制器中的代码:
puts "... #{ Tour.upcoming.order('start_date ASC').to_sql }"
@tours = Tour.upcoming.order('start_date ASC')
以及旅游模式中的范围
class Tour < ActiveRecord::Base
attr_accessible :start_date, :title
scope :upcoming, where('tours.start_date > ?', Date.today_local_zone)
end
这是我的日志中的一行(查询日期与日志中的日期不同)
- heroku重新启动后或部署日期是否正确
- 在heroku控制台
中,所有日期也都是正确的heroku运行rails c
- 我决定从头开始启动另一个应用程序,并在heroku上部署。结果是一样的
- 我用pingdom每5分钟ping一次这个页面。日期在一小时或两小时后,甚至在午夜23点后变得正确。i、 e.滞后值每天都不同
Time.zone.now.to\u s.to\u date的值。它的值是正确的
请同时查看以下内容:
- Gemfile
- 网页截图,包括生成的查询的所有值和文本
我在第一次复习时错过了这个。问题在您的范围声明中:
scope :upcoming, where('tours.start_date > ?', Date.today_local_zone)
这个定义是错误的。加载类时,它将加载Date.today\u local\u区域。你需要把它包装在一个进程中。因此,它应该是:
scope :upcoming, lambda { where('tours.start_date > ?', Date.today_local_zone) }
确保每次使用范围时都执行Date.today\u local\u区域。开始日期列的类型是什么?ping页面是否始终显示UTC的时区?Time.now是否始终显示“+0000”,或是否显示其他偏移量1)开始日期的类型是日期。2) 对。3) 是的,时间。现在总是显示“+0000”这里的评论-可能是相关的。你有任何宝石,可能会覆盖到_日期?如果将日期-本地时区方法更改为使用time.zone.now.to\u s.to\u Date
,会发生什么情况?这是我的GEM文件。我创建了一个简单的应用程序来重现错误。所以Gemfile非常简单,正如我所想,没有任何可能改变日期行为的宝石。Gemfile中没有明显的东西。您是否尝试过我上面建议的to_s
选项?
scope :upcoming, where('tours.start_date > ?', Date.today_local_zone)
scope :upcoming, lambda { where('tours.start_date > ?', Date.today_local_zone) }