Counter_cache with conditions? [Store non-spam comments #]

Hi guys.

Ok, I have a post model, and a comments model (Very similar to the
migrations screencast on the RoR website). I use a counter cache to
store the number of comments each post has with the post record.

I have just added Akismet anti-spam to my blog app, and am storing the
spam comments in the database until I have a change to manually verify
their spam-worthiness. The spam and non-spam comments are differentiated
by a boolean column called is_spam.

Thus I now want my counter_cache in the post model to only count those
comments that are NOT marked as spam (When a user clicks on a link that
says ‘5 comments’ only to discover there are none because they are all
spams, they are going to be slightly confused.

Any way I can achieve this?

Dougal - 3 weeks on Rails.

Douglas S. wrote:

Any way I can achieve this?

Not really one to wait around I came up with my own solution using a
separate column in the posts table, and after_save and after_destroy
methods to update this in my comment model.

I made a post at my blog:
http://douglasfshearer.dyndns.org/blog/custom-counter_cache-with-conditions

Thanks.

Dougal.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs