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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)