# Counter\_cache through, is this possible?

**URL:** <https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419>\
**Category:** Rails\
**Created:** [January 8, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419 "2008-01-08T23:18:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [January 8, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419/1 "2008-01-08T23:18:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [January 8, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419/2 "2008-01-08T23:52:58Z")

</div>

has\_many :cs, :through =\> :b, :counter\_cache =\> true

Does this work?

On Jan 9, 2008 8:48 AM, DEfusion [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

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

---

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [January 9, 2008, 12:11am UTC](https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419/3 "2008-01-09T00:11:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [January 9, 2008, 12:31am UTC](https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419/4 "2008-01-09T00:31:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [January 9, 2008, 12:43am UTC](https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419/5 "2008-01-09T00:43:54Z")

</div>

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

On Jan 9, 2008 9:59 AM, DEfusion [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.frozenplague.net)  
> > > Feel free to add me to MSN and/or GTalk as this email.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

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

---

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [January 9, 2008, 12:28am UTC](https://www.ruby-forum.com/t/counter-cache-through-is-this-possible/125419/6 "2008-01-09T00:28:06Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 8, 10:51 pm, “Ryan B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > association with B would do - but I don’t think that exists. Any  
> > > ideas?
> > 
> > –  
> > Ryan B.[http://www.frozenplague.net](http://www.frozenplague.net)  
> > Feel free to add me to MSN and/or GTalk as this email.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

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