Rails 3: Update counter_cache only if published
Asked Answered
F

1

6

This is a tricky question.

I have a project with multiple content types, such as Article, Review, etc. Each content type has comments, comments are polymorphic as they can belong to any content type.

I want to put a total comments counter on all my content pages to show how many comments there are, and I implemented a counter cache to do this with. (Something like @article.comments.count is more queries, and as I'm using the Ancestry gem for threaded comments, the child comments aren't counted in that number, only the root ones.)

The counter cache works great and shows the accurate number of comments, but as always there's one little catch. Comments aren't always published right away, only registered users can immediately publish, and these comments have a :status of '2'. Unregistered users get put into the moderation queue; these comments have a :status of '1'.

Problem is, the counter cache counts them all the same, so if you have four comments in moderation and one approved comment, the total count is 5.

Is there a way to add an exception onto the counter cache to tell it not to increment if the comment.status is not '2'? Likewise, when in the backend in the comments resource and setting that comment to publish (giving it status of 2), is there a way then (model, controller or otherwise) to make the counter cache for it's associated polymorphic / content type increment?

comments.rb

 # Comments
 has_many :comments, :as => :commentable, :dependent => :destroy
 accepts_nested_attributes_for :comments

article.rb

belongs_to :commentable, :polymorphic => TRUE, :touch => TRUE, :counter_cache => TRUE 
Fondle answered 2/5, 2011 at 2:8 Comment(0)
B
5

What I can think of at the moment is, you may have to provide your own counter cache columns in the model and increment/decrement as required. i.e.

active_comments_count
pending_comments_count

Then in your model, you can trigger your update_comment_count method via a callback.

Its more of an effort on your side, but I am sure this approach will work.

Bright answered 2/5, 2011 at 5:0 Comment(1)
After thinking about this for a while, I think you're right. I might even go a step further and abandon counter caching for this, and using total submitted comment and total published comment columns. Reason being is that I plan on implementing a community reputation system someday, and a published-to-total ratio would be very helpful to have. Thanks.Fondle

© 2022 - 2024 — McMap. All rights reserved.