我在Heroku上托管了一个Rails 3应用程序,它运行在Postgres数据库上。我有一个应该生成一个RSS提要的控制器动作,但是我发现它在应用程序重新启动之前从不更新(或者因为我为Heroku做了一个新的发布,或者因为该站点一段时间不活动,所以dyno旋转下来并再次备份)。它正在返回排除新项目的“陈旧”数据。Date.current上的作用域仅在Rails重新启动时更新
范围定义像这样的基础上,在我的模型行之间的比较称为date
和Date.current
:
class Petition < ActiveRecord::Base
scope :current, where(["petitions.date <= ?", Date.current]).order("petitions.date DESC")
scope :published, { :conditions => { :published => true } }
end
控制器操作是这样的:
class PetitionsController < ActionController::Base
before_filter :find_diary
def rss
current_petitions.each do |petition|
puts petition.date
end
end
protected
def find_diary
@diary = Diary.find(params[:id])
end
def current_petitions
@diary.petitions.published.current.limit(25)
end
end
,你可以请参阅,它应该返回所有请求与今天或更早的date
。它在Heroku控制台上工作正常:Diary.find(15).petitions.published.current.limit(25).first
总是给我一个今天。我可以从日志中看到,每次请求提要时它都会调用该函数的current_petitions
函数。但如果我离开网站24小时运行,那么它仍然只会显示我的请求,直到昨天。如果我做一个发布来添加任何调试代码,突然它会再次开始工作。
很容易,当你知道如何! – andygeers