Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: Update counter_cache only if published

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 
like image 302
Shannon Avatar asked May 02 '11 02:05

Shannon


1 Answers

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.

like image 176
Christian Fazzini Avatar answered Nov 04 '22 11:11

Christian Fazzini