Ruby on Rails Thursday, November 6, 2014

This is really simple, but surprisingly effective: If you already have post.comments, then call post.comments.length on it instead of post.comments.count on it and it won't make another SELECT COUNT(*) query. 

Otherwise, counter cache. 


On Wednesday, November 5, 2014 11:40:42 PM UTC-7, Logesh m wrote:
I have a rails app where I need to take count of the associated model like post.comments.count and this loads up number of queries and I was searching for option to reduce this and found counter cache which saves the count in the table but is there any other option to find the count without saving in the table?

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/79fa3fd7-766e-42ed-a5a8-f800ff72bfe9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment