Another option is to set a cron job to count postings at a regular
time interval and update the counter in the database.  That way, an
sql count won't be done after every update or removal.  Keeping track
of postings by incrementing or decrementing the count each time an
action is performed could lead to an inaccurate count if not every
action is accounted for, i.e. a posting is removed by the user,
reposted by the user, flagged for removal, etc.


On Dec 10, 8:57 am, Patrick Crowley <[email protected]> wrote:
> > 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?
>
> Well, I was suggesting to use named_scope as a replacement for counter_cache, 
> especially if your application only needs counter_cache behavior for content 
> administration.
>
> > 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.
>
> You could start by adding a single custom counter_cache for spam to the 
> Posting model.
>
> Then add a named_scope to the Posting model that grabs any Postings that have 
> been flagged as spam (according to the custom counter_cache you added).
>
> Then, to pull up the spam Postings for each category, you just need to tweak 
> your ActiveRecord finder to do the right thing (probably by using some 
> combination of :conditions, :join, :include, and maybe :group).
>
> Posting.flagged_as_spam.count(:conditions => blah, :include => blah)
>
> This is incomplete, but you get the idea.
>
> -- Patrick

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

Reply via email to