RoR - How to count and display comments

I am looking to count the number of comments left in an article and display them on the index page next to this particular article - for example, an example in red circles. Any suggestions on how I can do this?

Image is an example of what I'm trying to do, this is not my site.

enter image description here

+3
source share
2 answers

That sounds like a good candidate for Rails.cache. Each time you create a new comment, simply increase the cache counter using the post identifier.

If the cache entry does not exist, run a simple query article.comments.count(depending on your domain model) and re-cache it.

+1
source

- , .

counter_cache, , . , , . . http://guides.rubyonrails.org/association_basics.html, 4.1.2.4.

0

All Articles