Thanks, Patrick.  So how could one implement a counter cache in the
database using named scopes so that the count function is not called
each time you want a count?  The only way I see to do this is to have
separate flags tables to join to for each category of flag you would
like to keep track of and have a counter cache in each of those
tables, i.e. a table for flagged as spam postings, a table for flagged
as irrelevant, etc.  But then that could become messy in itself
because say you want to count the number of flagged as spam postings
in a posting category of "Weather Discussions", a posting category of
"Job Discussions" category, etc.

-- Cheri

On Dec 8, 6:55 pm, Patrick Crowley <[email protected]> wrote:
> 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