On memoizing methods that return a scope

The memoize method has long since been removed from ActiveSupport. Please use the memoizer gem which doesn't suffer from the issue below.

Be careful when memoizing Show archive.org snapshot a method that returns a scope, e.g.:

def variants
  scoped(:conditions => { :name => name })
end

memoize :variants

Because of the way memoize is implemented, that method now no longer returns a scope but its loaded target array.

The best solution is to use the Memoizer gem instead.

A workaround is to roll your own memoization:

def variants
  @variants ||= scoped(:conditions => { :name => name })
end
Henning Koch Over 13 years ago