# Counter\_cache won't increment

**URL:** <https://www.ruby-forum.com/t/counter-cache-wont-increment/90740>\
**Category:** Rails\
**Created:** [March 31, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/counter-cache-wont-increment/90740 "2007-03-31T18:31:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [March 31, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/counter-cache-wont-increment/90740/1 "2007-03-31T18:31:28Z")

</div>

I’m having an odd problem. I’ve pinpointed what’s causing it, but it’s  
not making any sense.

class Discussion \< ActiveRecord::Base  
has\_many :posts, :dependent =\> :destroy

def bump  
update\_attribute(:bumped\_on, Time.now)  
end  
end

class Post \< ActiveRecord::Base  
belongs\_to :discussion, :counter\_cache =\> true

after\_create :bump\_discussion

def bump\_discussion  
discussion.bump  
end  
end

Now I can do something obvious like  
post = Post.new  
post.discussion = Discussion.find(:first)  
post.save

But this won’t increment posts\_count in the discussions table. HOWEVER,  
if I remove the bump\_discussion method then posts\_count DOES get  
updated. This doesn’t really make any sense. My guess is that updating  
the row one way is overriding the other update.

Furthermore, calling Post.destroy\_all seems to show that things would be  
working, but obviously MySQL chokes since I am trying to have it  
decrement 0 by 1, and the column is an unsigned integer (no negative  
numbers.)

What’s up with this?

---

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [April 1, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/counter-cache-wont-increment/90740/2 "2007-04-01T22:38:29Z")

</div>

Eleo wrote:

> Now I can do something obvious like  
> post = Post.new  
> post.discussion = Discussion.find(:first)  
> post.save
> 
> But this won’t increment posts\_count in the discussions table. HOWEVER,  
> if I remove the bump\_discussion method then posts\_count DOES get  
> updated. This doesn’t really make any sense. My guess is that updating  
> the row one way is overriding the other update.

Currently counter\_cache fields are not excluded from being updated  
when a save is done, so the updated count, written directly to the DB,  
is getting overwritten by the old count in the Discussion object.

The most simple solution is to rewrite bump as

def bump  
update\_all “bumped\_on = #{self.class.sanitize(Time.now)}”, “id =  
#{id}”  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![eleo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@eleo](https://www.ruby-forum.com/u/eleo)\
**Post date:** [April 2, 2007, 2:13am UTC](https://www.ruby-forum.com/t/counter-cache-wont-increment/90740/3 "2007-04-02T02:13:18Z")

</div>

Sigh, snags.

Thanks for answering. Your solution worked fine.
