2013-07-17 27 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

相關問題