On memoizing methods that return a scope

Updated . Posted . Visible to the public. Deprecated.

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
Last edit
Henning Koch
Keywords
memoize
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2010-10-20 14:17)