Flowdock

Good notes posted by actsasflinn

RSS feed
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