scytherswings said almost 7 years ago on Counter Cache Associations :

I feel like the video skipped over the

company.users.count

method and its behavior. Could you add that to the samples below?
Cheers,
-Andrew


David Kimura PRO said almost 7 years ago on Counter Cache Associations :

count will perform similar to size. However, for the life of the object (typically just the instance of the request), it will perform another Count(*) query. So, if you are getting the count of an object multiple times within a view or method of that instance, it would make multiple database calls.

length can actually be fast if you have already eager loaded the objects from the association. However, for the most part, I avoid length.

size is similar to count with the exception that it will "cache" the Count(*) query or perform it if necessary. It's basically like a memoization of the count.


scytherswings said almost 7 years ago on Counter Cache Associations :

I didn't realize that `.count` was so inefficient, looks like I have some optimization to do. Thanks for the information!


Login to Comment