Read more

On memoizing methods that return a scope

Henning Koch
October 20, 2010Software engineer at makandra GmbH

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
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

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
Posted by Henning Koch to makandra dev (2010-10-20 16:17)