2013-06-05 43 views
0

在我的Rails模型中,我需要調用after_commit回調,當且僅當其中一個表屬性從一個值更改爲其他值時。
這與after_update回調正常工作,但我在後臺調用一些額外的任務和after_commit是完美的一個。有什麼建議麼?提交條件後的Rails

示例代碼

after_commit :call_cli, :if=> :change 

    def change 
     if self.status_changed? 
     filename = '/location/res.log'    
     File.open(filename, File::WRONLY) do |file| 
      file.write self.status_change_was+"\n" 
     end 
     end 
    end 
+0

** ** after_commit完美執行。但我的要求是,如果表屬性更改,它應該運行。讓我提供更多細節。 – ejo

+0

** after_commit **正在處理掉自身對象被銷燬的情況。我在這裏掙扎,因爲如果屬性值改變,我可以調用CLI。你能幫我拿一些示例代碼嗎? – ejo

+0

我發現[這個問題](http://stackoverflow.com/questions/7128073/after-commit-for-an-attribute)關於條件'after_commit',以防你想檢查一些其他選項。 –

回答