Forum: Ruby on Rails Problem with counter cache in many-to-many relationship

916a6c67f3d61d3bc9b2273646e53a0b?d=identicon&s=25 Fabian Becker (Guest)
on 2012-09-13 15:52
(Received via mailing list)
Hi guys,
I am having trouble getting to counter cache to work properly with a
many-to-many relationship. What I have done is:

rails g scaffold post title:string body:text
rails g scaffold tag name:string taggings_count:integer
rails g model tagging post_id:integer tag_id:integer

Then I created the relationship as shown in my Gist:
The idea is to have an input field when creating a post, where tags can
entered seperated by comma. The tag_names method then splits those tags,
adds new ones and deletes the ones that are now missing.

The counter_cache is correctly increased when adding a tag to the list.
However, decreasing the number after a tag removal does not work.

I have done some searching and found several blog articles (e.g.
explaining that this was a bug in Rails 3.0.x that has been addressed in
edge rails. See the old bug report on lighthouse:
then the fix on Github:

Why am I still having this problem, when it was fixed two years ago? Am
missing something here?

# rails -v
Rails 3.2.8

Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.