# Counter cache not decrementing

**URL:** <https://www.ruby-forum.com/t/counter-cache-not-decrementing/194130>\
**Category:** Rails\
**Created:** [August 30, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/counter-cache-not-decrementing/194130 "2010-08-30T21:03:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 30, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/counter-cache-not-decrementing/194130/1 "2010-08-30T21:03:40Z")

</div>

This is what I have

```
belongs_to :user, :counter_cache => true
belongs_to :vote_item, :counter_cache => true
belongs_to :vote_topic, :counter_cache => true

```

##User model

def vote\_for(vid, vtid)  
Vote.create(:user\_id =\> self.id, :vote\_item\_id =\>  
vid, :vote\_topic\_id =\> vtid)  
end

```
def cancel_vote(vid, vtid)
    Vote.find(:first, :select => "id", :conditions =>

```

[‘vote\_item\_id = ? AND vote\_topic\_id = ? AND user\_id = ?’, vid, vtid,  
self.id]).destroy  
end

When a vote is created the votes\_count in VoteItem and VoteTopic are  
updated correctly. But when a vote is destroyed or cancelled the  
counter is not decremented.

Please help

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 30, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/counter-cache-not-decrementing/194130/2 "2010-08-30T21:17:43Z")

</div>

On Aug 30, 8:02 pm, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> vid, :vote\_topic\_id =\> vtid)  
> counter is not decremented.

Stab in the dark: because your select clause only includes id, rails  
doesn’t know the user\_id/vote\_item\_id/vote\_topic\_id and so doesn’t  
know which user/vote\_item/vote\_topic to update.

Fred

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [August 31, 2010, 2:00am UTC](https://www.ruby-forum.com/t/counter-cache-not-decrementing/194130/3 "2010-08-31T02:00:35Z")

</div>

Bulls eye! That was it

On Aug 30, 12:16 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
