Reputation: 456
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
Upvotes: 1
Views: 5423
Reputation: 7135
There's actually a previous changes
method, which I would recommend: http://api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-previous_changes
Upvotes: 2
Reputation: 456
Got it. Called another callback before_update and self.status_changed? value is stored in a flag as @flag = self.status_changed?
Thanks to this article - http://axonflux.com/offlining-denormalized-tasks-gotchas-with-off.
Also cheers to Dave Newton for the kind help.
Upvotes: 0