# Counter cache problem

**URL:** <https://www.ruby-forum.com/t/counter-cache-problem/162246>\
**Category:** Rails\
**Created:** [March 7, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/counter-cache-problem/162246 "2009-03-07T13:24:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [March 7, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/counter-cache-problem/162246/1 "2009-03-07T13:24:27Z")

</div>

Here is the problem  
There is a gallery model and image model  
gallery has many images  
gallery has images\_count.

When I change image from one gallery to another i have to update the  
counter cache(It does not happen automatically on doing  
gallery2.images \<\< image).  
So i do  
gallery1.images\_count -= 1  
gallery2.images\_count += 1  
gallery1.save  
gallery2.save

but this still does not update the count of the images

when i see the log files, the update queries are fired, but they  
update only the updated\_at column and not the images\_count column.  
The same thing works when done through console.  
Can any one explain why this is happening?

Regards,  
Pankaj

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [March 7, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/counter-cache-problem/162246/2 "2009-03-07T13:32:04Z")

</div>

Gallery.rb  
has\_many :images, :counter\_cache =\> true

On Sat, Mar 7, 2009 at 8:23 PM, pankaj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> gallery1.images\_count -= 1
> 
> Regards,  
> Pankaj
> 
> >

–  
Only two surfaces of a box:  
[http://blog.pixnet.net/zusocfc](http://blog.pixnet.net/zusocfc)

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [March 7, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/counter-cache-problem/162246/3 "2009-03-07T17:20:32Z")

</div>

I have already added :counter\_cache with the belongs to relationship,  
it cannot be added with has\_many.  
Any ways I am updating the counter manually, and it is not happening.  
Regards,  
Pankaj
