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