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.
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. :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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...
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. :)