Flowdock
method

after_save

Importance_3
v2.1.0 - Show latest stable - 3 notes - Class: ActiveRecord::Callbacks
after_save() public

Is called after Base.save (regardless of whether it’s a create or update save).

 class Contact < ActiveRecord::Base
   after_save { logger.info( 'New contact saved!' ) }
 end
Show source
Register or log in to add new notes.
July 28, 2009
8 thanks

Return True

As is the case with the before_validation and before_save callbacks, returning false will break the callback chain. For example, the expire_cache_id method will not run if Rails.cache.expire returns false (as it will if the key is not cached with memcache).

Returning False Example (Bad)

after_save :expire_cache_by_name
after_save :expire_cache_by_id

def expire_cache_by_name
  Rails.cache.expire("my_object:name:#{self.name}")
end

def expire_cache_by_id
  Rails.cache.expire("my_object:#{self.id}")
end

Returning True Example (Good)

def expire_cache_by_name
  Rails.cache.expire("my_object:name:#{self.name}")
  return true
end

def expire_cache_by_id
  Rails.cache.expire("my_object:#{self.id}")
  return true
end
March 10, 2014
1 thank

gives a parameter

As a note, you can use it like this:

after_save {|instance|

}

it will pass in the instance being saved.

September 7, 2011 - (v3.0.0 - v3.0.9)
0 thanks

Tested w/ Rails 3 and returning true/false ignored

Per http://ar.rubyonrails.org/classes/ActiveRecord/Callbacks.html “If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled.” [and presumably associated action is not canceled]

So, the callback chain will be broken, but the save will still happen.

Also ran into this: http://innergytech.wordpress.com/2010/03/08/dont-put-validations-in-after_save/