Skip to content

Instantly share code, notes, and snippets.

@andreypronin
Created April 3, 2013 22:43
Show Gist options
  • Save andreypronin/5306143 to your computer and use it in GitHub Desktop.
Save andreypronin/5306143 to your computer and use it in GitHub Desktop.
Using previous_changes after after_save, in after_commit. Taken from http://logicalfriday.com/2012/08/21/rails-callbacks-workers-and-the-race-you-never-expected-to-lose/: Available on every object is a method called previous_changes which contains a hash of attributes that changed the last time the object was saved. Sadly, with this we don’t get…
class User < ActiveRecord::Base
after_commit :update_external_email, :if => :email_previously_changed?
private
def update_external_email
# do whatever you need to do
end
def email_previously_changed?
previous_changes[:keys].include?('email')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment