Flowdock

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
October 30, 2008
0 thanks

Anyone know the order the scopes assemble conditions?

It seems like last scope = first condition in sql. Can anyone confirm?

October 24, 2008
1 thank

module includes with callbacks

If you write a plugin or module that includes callbacks make sure to define the method and call super after you’re done with your business.

module CoolStuff

def self.included(base)
  super
  base.extend(ClassMethods)
  # the next line seems to clobber. instead opt for defining an inheritable method
  # base.after_save :chill
end

module ClassMethods
  # cool class methods
end

def chill
  self.cool = true
end

def after_save
  self.chill
  super # if you don't call super, bloggy won't run
end

end # yes I know this next line is a divisive issue but it’s common enough ActiveRecord::Base.send :include, CoolStuff

class Blog < ActiveRecord::Base

after_save :bloggy

def bloggy
  slugify_title
end

end