The link I sent just has one method that's relevant -
add_counter_cache_callbacks - starting on line 1431.  Dynamically defines
the after_create and before_destroy callback methods and
increments/decrements the counter cache column unless the association is
nil.

The link you provided will work, but doesn't scale.  You'll be calling a
nasty count query each time a record is saved, when you should just use the
increment and decrement methods provided.

You're not going to get too far with Rails if you're not willing to browse
the source that makes the magic happen.

On Tue, Dec 8, 2009 at 2:53 PM, liquid_rails <[email protected]>wrote:

> This shows how to do it too w/o having to read through a lot of code.
> I'll try it.  Thanks for you help!
>
> http://douglasfshearer.com/blog/custom-counter_cache-with-conditions
>
> On Dec 8, 2:49 pm, liquid_rails <[email protected]> wrote:
> > Both.  I haven't tried to implement it yet.  I just clicked on the
> > link you sent and there's lots of code to go through!  I DO think a
> > plugin would be useful.  I don't know how to write plugins.  I'm not
> > full-time at this.
> >
> > On Dec 8, 2:43 pm, James Miller <[email protected]> wrote:
> >
> >
> >
> >
> >
> > > Are you still having trouble implementing a custom one?  Or just
> commenting
> > > that it would be helpful?
> >
> > > Why don't you write one?
> >
> > > On Tue, Dec 8, 2009 at 2:38 PM, liquid_rails <
> [email protected]>wrote:
> >
> > > > If someone could write a plugin to do custom counter-caches, that
> > > > would be Awesome.  I think it would be very useful!
> >
> > > > On Dec 8, 10:34 am, James Miller <[email protected]> wrote:
> > > > > There's no built-in functionality for this, but you can just add
> > > > > after_create and before_destroy callbacks to increment/decrement
> the
> > > > parent
> > > > > records with the correct count based on any number of conditions.
> >
> > > > > You can see how it's implemented in the Rails source here:
> > > >
> http://github.com/rails/rails/blob/master/activerecord/lib/active_rec...
> >
> > > > > On Tue, Dec 8, 2009 at 10:28 AM, liquid_rails <
> [email protected]
> > > > >wrote:
> >
> > > > > > I would like for the database to keep a count of postings
> belonging to
> > > > > > a specific category (can do that), that are NOT expired (don't
> know
> > > > > > how to do that).   Are there any built-in functions to do this?
> >
> > > > > > Thanks!
> >
> > > > > > --
> > > > > > SD Ruby mailing list
> > > > > > [email protected]
> > > > > >http://groups.google.com/group/sdruby
> >
> > > > --
> > > > SD Ruby mailing list
> > > > [email protected]
> > > >http://groups.google.com/group/sdruby
> >
> >
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
>

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

Reply via email to