Counter_cache through, is this possible?

Say I have A which has many B which has many C, ideally I want the
quickest way to get the count of C for A, a counter cache through an
association with B would do - but I don’t think that exists. Any ideas?

has_many :cs, :through => :b, :counter_cache => true

Does this work?

On Jan 9, 2008 8:48 AM, DEfusion [email protected] wrote:

Say I have A which has many B which has many C, ideally I want the
quickest way to get the count of C for A, a counter cache through an
association with B would do - but I don’t think that exists. Any ideas?


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

No the counter_cache is on the belongs_to and if I try adding through
on the belongs to then it whinges, e.g. on C:

belongs_to :a, through => :b, :counter_cache => true

I get the error undefined local variable or method ‘through’

-D

What I’ve ended up doing is the following

class C < ActiveRecord::Base
belongs_to :B

after_create :increment_A_counter_cache
after_destroy :decrement_A_counter_cache

private

def increment_A_counter_cache
    A.increment_counter( 'c_count', self.B.A.id )
end

def decrement_A_counter_cache
    A.decrement_counter( 'c_count', self.B.A.id )
end

end

It seems to work, I was just wondering if there was a way to do it
through the framework.

-D

Not currently. Do you feel confident enough to suggest this as a patch
to
rails?

On Jan 9, 2008 9:59 AM, DEfusion [email protected] wrote:

through the framework.

Say I have A which has many B which has many C, ideally I want the
quickest way to get the count of C for A, a counter cache through an
association with B would do - but I don’t think that exists. Any
ideas?


Ryan B.http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Maybe it should be :through and not through? You’re missing a colon. But
I
dont think belongs_to has a through option.

On Jan 9, 2008 9:41 AM, DEfusion [email protected] wrote:

On Jan 8, 10:51 pm, “Ryan B.” [email protected] wrote:

association with B would do - but I don’t think that exists. Any
ideas?


Ryan B.http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.