DEV Community

Discussion on: Ruby/Rails : Memoization with dynamically defined methods

Collapse
 
rhymes profile image
rhymes

One detail: if you store the result of the User.all in an instance variable in the controller and access it from the template, Rails does not go back to the DB twice, so you might not need memoization after all.

In addition, ActiveRecord caches results by itself in some cases: guides.rubyonrails.org/association...

Collapse
 
ritikesh profile image
Ritikesh

You are right, ActiveRecord does cache results, but not always. Like when you use scopes or self methods or finder methods etc. In most real-world use cases, you wouldn't do a User.all anyway. I'll try to update to a better example sometime later, but the gist was to suggest how to memoize dynamic methods. :)