method
after_save
rails latest stable - Class:
ActiveRecord::Callbacks
after_save()public
Is called after Base.save (regardless of whether it’s a create or update save). Note that this callback is still wrapped in the transaction around save. For example, if you invoke an external indexer at this point it won’t see the changes in the database.
class Contact < ActiveRecord::Base after_save { logger.info( 'New contact saved!' ) } end
2Notes
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
gives a parameter
As a note, you can use it like this:
after_save {|instance|
}
it will pass in the instance being saved.