0

In my rails model I need to call after_commit callback if and only if one of the table attributes is changed from one value to other.
This is working fine with after_update callback but I am invoking some additional tasks in the background and after_commit is the perfect one for this. Any suggestions?

Sample code

    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
4

2 回答 2

1

实际上有一种previous changes方法,我会推荐:http ://api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-previous_changes

于 2013-11-27T00:19:49.040 回答
0

知道了。调用了另一个回调before_update和 self.status_changed?值作为@flag = self.status_changed 存储在标志中?
感谢这篇文章 - http://axonflux.com/offlining-denormalized-tasks-gotchas-with-off也为戴夫牛顿
的帮助 而欢呼。

于 2013-06-06T07:45:01.793 回答