2013-07-17 26 views
0

我在Heroku上托管了一个Rails 3应用程序,它运行在Postgres数据库上。我有一个应该生成一个RSS提要的控制器动作,但是我发现它在应用程序重新启动之前从不更新(或者因为我为Heroku做了一个新的发布,或者因为该站点一段时间不活动,所以dyno旋转下来并再次备份)。它正在返回排除新项目的“陈旧”数据。Date.current上的作用域仅在Rails重新启动时更新

范围定义像这样的基础上,在我的模型行之间的比较称为dateDate.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小时运行,那么它仍然只会显示我的请求,直到昨天。如果我做一个发布来添加任何调试代码,突然它会再次开始工作。

回答

2

当您在其中设置时间范围时,您需要使用lambda表达式以确保每次使用范围时都会评估时间调用。在rails guide有更多的细节,但你的范围需要看起来更像:

scope :current, lambda { where(["petitions.date <= ?", Date.current]) } 
+0

很容易,当你知道如何! – andygeers

相关问题