Posted almost 12 years ago. Visible to the public. Deprecated.

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 Archive a method that returns a scope, e.g.:

Copy
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:

Copy
def variants @variants ||= scoped(:conditions => { :name => name }) end
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 5 years ago
by Henning Koch
Keywords:
memoize
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more