method

after_save

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

2Notes

Return True

actsasflinn · Jul 28, 20098 thanks

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

rogerdpack · Mar 10, 20141 thank

As a note, you can use it like this:

after_save {|instance|

}

it will pass in the instance being saved.