# Couter Cache for 2 Parents?

**URL:** <https://www.ruby-forum.com/t/couter-cache-for-2-parents/52549>\
**Category:** Rails\
**Created:** [January 16, 2006, 9:32am UTC](https://www.ruby-forum.com/t/couter-cache-for-2-parents/52549 "2006-01-16T09:32:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Tseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@Tony\_Tseng](https://www.ruby-forum.com/u/Tony_Tseng)\
**Post date:** [January 16, 2006, 9:32am UTC](https://www.ruby-forum.com/t/couter-cache-for-2-parents/52549/1 "2006-01-16T09:32:00Z")

</div>

Hi,  
I’ve got a question about counter cache; here’s my setup:  
I have 3 model classes, A, B, and C.  
A has many C.  
B has many C.  
C belongs to both A and B.

Now I’d like to keep counters of C in both A and B for performance  
reasons.  
According to the rails book, this doesn’t seem to be possible, as the  
counter  
feature requires changes to be made through the object that maintains  
the  
collection (either A or B). So my question is, what’s the best way to  
accomplish  
this? Has anybody solved this issue before?

Cheers,  
Tony

---

<div class="post-metadata">

**Author:** ![Tony\_Tseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@Tony\_Tseng](https://www.ruby-forum.com/u/Tony_Tseng)\
**Post date:** [January 16, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/couter-cache-for-2-parents/52549/2 "2006-01-16T15:59:27Z")

</div>

I don’t understand it. If you have C belongs to A and B, can’t you do a  
counter-cache for both?

If you do:

a = A.new  
b = B.new  
c = C.new

a.cs \<\< c  
b.cs \<\< c

That will increment both counters, or not?

Jules

---

<div class="post-metadata">

**Author:** ![Tony\_Tseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@Tony\_Tseng](https://www.ruby-forum.com/u/Tony_Tseng)\
**Post date:** [January 16, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/couter-cache-for-2-parents/52549/3 "2006-01-16T19:55:07Z")

</div>

That’s the weird part. When I tried:  
a.cs \<\< c  
b.cs \<\< c

The result I got is:  
a.cs\_count = 1  
b.cs\_count = 2

However, if I access b.cs\_count right before b.cs \<\< c, then the count  
is correct. (e.g., print out the value of b.cs\_count before adding c to  
b.cs)

I must be missing something here. Any suggestions?

Jules wrote:

> I don’t understand it. If you have C belongs to A and B, can’t you do a  
> counter-cache for both?
> 
> If you do:
> 
> a = A.new  
> b = B.new  
> c = C.new
> 
> a.cs \<\< c  
> b.cs \<\< c
> 
> That will increment both counters, or not?
> 
> Jules

---

<div class="post-metadata">

**Author:** ![Tony\_Tseng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@Tony\_Tseng](https://www.ruby-forum.com/u/Tony_Tseng)\
**Post date:** [January 17, 2006, 2:01am UTC](https://www.ruby-forum.com/t/couter-cache-for-2-parents/52549/4 "2006-01-17T02:01:10Z")

</div>

I have this same setup, and here’s what worked for. Basically I added  
the object a bit differently to the parents.  
All I did for new new object was to set the parent instead of adding  
it as a child. So in your case:

c.a\_type = a  
c.b\_type = b

c.save

And the counts got updated correctly. It worked for me. And doing  
c.destroy got decremented the count as needed.

-Nick
