Forum: Ruby on Rails Association count caching

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jon L. (Guest)
on 2006-03-27 00:24
Hi all,

5.times { foo.association }

will produce 1 query to the DB; the result is "cached" (for want of a
better word) in a variable somewhere. However:

5.times { foo.association.count }

will run the query 5 times. Why?

I could use an association extension to cache the count value for me,
but first I want to understand whether this is a bad idea and has been
deliberately avoided?

This topic is locked and can not be replied to.