Weird... I was dealing with the same exact issue today.

As you mentioned, this blog post has a good technique for custom counters:
http://douglasfshearer.com/blog/custom-counter_cache-with-conditions

It works well, and I had it running in my app earlier today. For a single, 
one-off custom counter, that's the way to go.

But in my case, I needed about half a dozen counters for a backend admin 
interface. Since the admin interface was not user-facing and will manage less 
than 5,000 articles of content, creating so many counter caches seemed like 
overkill.

So I looked for alternatives... and now I'm using named_scopes:

class Post < ActiveRecord::Base
  named_scope
  :flagged_as_spam,
  :joins => :flags,
  :conditions => "flags.reason = 'Spam'",
  :group => "posts.id"
end

I've got a Posts table and a Flags table (for when users flag content as spam, 
etc.). With this named scope on the Post model, it's now easy to find blog 
posts that have been flagged as spam.

@spam_posts = Post.flagged_as_spam.paginate :page => params[:page]

-- Patrick

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to