2
votes

I'm working with a Ruby on Rails application where it was authored to generate some files in the after_save callback. The paperclip gem is used in the class. When these files are created, save is called on the Ruby object which triggers a loop of this second save calling after_save which calls save which calls after_save and on and on.

save! -> after_save -> save! -> after_save!

Eventually somehow the app breaks out of this loop on its own. I'm not sure how.

My question is, if I have an attribute on a class that is changed?=true, when does changed? get flipped to false? We use a changed? check as a sort of gate to prevent this file creation from happening and it seems like changed is always true every time it enters the after_save code. I would've thought that in after_save, changed? would be false by now since the data has been saved.

1
after_update() can be use after you calling the after_saveTushar Pal

1 Answers

-1
votes

You should use after_commit with a condition of the key (attribute) present in the previous_changes. http://apidock.com/rails/ActiveModel/Dirty/previous_changes