This project is archived and is in readonly mode.
[Patch] has_many :through doesn't update counter_cache on join model correctly.
If you have a counter_cache on a join model used in a has_many :through association, when removing items from the association, the counter_cache doesn't get decremented correctly. e.g.
class Post < ActiveRecord::Base has_many :taggings has_many :tags, :through => :taggings end class Tagging < ActiveRecord::Base belongs_to :post, :counter_cache => true belongs_to :tag end class Tag < ActiveRecord::Base has_many :taggings has_many :posts, :through => :taggings end post = Post.create!() tag1 = Tag.create!() tag2 = Tag.create!() post.tags << tag1 post.reload.taggings_count # => 1 post.tags = [tag2] post.reload.taggings_count # => 2 ! Should be 1
This happens because #delete_records in HasManyThroughAssociation does a delete_all. The attached patch changes this to a destroy_all which fixes the issue.
Comments and changes to this ticket
Unfortunately this patch does not apply cleanly for me.
It comes up with an error message
Applying Make has_many :through update counter_cache on join model correctly. error: patch failed: activerecord/test/cases/associations/has_many_through_associations_test.rb:81 error: activerecord/test/cases/associations/has_many_through_associations_test.rb: patch does not apply Patch failed at 0001. When you have resolved this problem run "git-am --resolved". If you would prefer to skip this patch, instead run "git-am --skip".
That works fine and updates taggings_count as expected.
This problem exists in edge rails as well. Is this change going to be somehow merged back there or patch needs to be created to deal with this issue in edge rails?
Don't be sorry Alex. It is me who didn't notice 2-3-stable in the tags for this ticket but I am very new to lighthouse and as a matter of fact to rails itself as well (and git :).
Ok, patches done. They're on github here: http://github.com/unboxed/rails/tree/2-3-counter-cache-fix and here: http://github.com/unboxed/rails/tree/edge-counter-cache-fix
Let me know if you'd prefer me to create actual patch files for these.
- Title changed from [PATCH] Clean up "omg" comments to [Patch] has_many :through doesn't update counter_cache on join model correctly.
- Assigned user set to Yehuda Katz (wycats)
- Tag changed from 3, documentation, patch, review to rails 3, 2-3-stable, bug, bugmash, counter_cache, has_many_through, patch
Reverting Spam changes...
I hit the same problem, using rails 2.3.
I identified another issue, look at this code:
class PlaylistTrack < ActiveRecord::Base belongs_to :playlist, :counter_cache => true, :touch => true belongs_to :track
1) adding an object to the collection using <<
- counter cache works
- playlist.updated_at fields gets correctly updated
2) **removing an object from the collection using collection.delete(object)
- counter cache won't work
- playlist.updated won't get updated
In the end there might be additional issues because Playlist might have an observer/callback that runs after_update code.
In the point 1 this after_update is not triggered by the rails code that updates the counter_cache, but from the :touch one, meaning that adding the :touch options also triggers correctly after_update callbacks.
Luckily it won't fire twice.
However in the point 2 since nothing is run also the after_update callback isn't run.
Any plans to integrate this patch please?
The current status of the has_many_through association and its dependent options is the following:
# TODO - add dependent option support def delete_records(records)
Also in the reference one can find this line:
Warning: This option is ignored when used with :through option.
I've implemented the method, and it works for me prerry well. Could anyone please test the attached patch? It turned out that it's rather hard to run the whole tests of active record...
I apply the path to the branch 3.0-stable (3.0.4rc)
class User < ActiveRecord::Base # need to provide an accessor , if not WARNING: Can't mass-assign protected attributes: role_ids attr_accessible :role_ids has_many :permissions has_many :roles, :through => :permissions end class Role < ActiveRecord::Base attr_accessible :name, :users_count has_many :permissions has_many :users, :through => :permissions end class Permission < ActiveRecord::Base belongs_to :role, :counter_cache => true belongs_to :user end
When i try to update my model (in a controller ) with
i got this error
update'<br/> rails.3.0.4rc/activerecord/lib/active_record/associations/has_many_through_association.rb:95:inblock in delete_records'
block in delete'<br/> rails.3.0.4rc/activerecord/lib/active_record/associations/association_collection.rb:525:inblock in remove_records'
block in transaction'<br/> rails.3.0.4rc/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:139:intransaction'
block in replace'<br/> rails.3.0.4rc/activerecord/lib/active_record/associations/association_collection.rb:158:inblock in transaction'
block in collection_accessor_methods'<br/> rails.3.0.4rc/activerecord/lib/active_record/associations.rb:1532:inblock in collection_accessor_methods'
block in attributes='<br/> rails.3.0.4rc/activerecord/lib/active_record/base.rb:1554:ineach'
attributes='<br/> rails.3.0.4rc/activerecord/lib/active_record/persistence.rb:127:inblock in update_attributes'
block in with_transaction_returning_status'<br/> rails.3.0.4rc/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:139:intransaction'
update_attributes'<br/> app/controllers/admin/users_controller.rb:141:inblock in update'
Nobody can look into this issue i m really stuck !
Thanks in advance.
look my previous stackstrace:
rails.3.0.4rc/activerecord/lib/active_record/associations/has_many_through_association.rb:95:inblock in delete_records'
rails.3.0.4rc/activerecord/lib/active_record/associations/association_collection.rb:222:in block in delete'
rails.3.0.4rc/activerecord/lib/active_record/associations/association_collection.rb:525:inblock in remove_records'
rails.3.0.4rc/activerecord/lib/active_record/associations/association_collection.rb:158:in block in transaction'
- State changed from open to resolved
(from [52f09eac5b3d297021ef726e04ec19f6011cb302]) Correctly update counter caches on deletion for has_many :through [#2824 state:resolved]. Also fixed a bunch of other counter cache bugs in the process, as once I fixed this one others started appearing like nobody's business. https://github.com/rails/rails/commit/52f09eac5b3d297021ef726e04ec1...
Apologies if this is not the correct place to post this but I would like some advice on the best way to integrate this in my project.
I am currently using rails 3.0.5 rather than rails edge.
Should I change my gemfile to point to the git repository as I am deploying to heroku.
Or is there an alternative? Perhaps cloning the git repository and manually copying the new files over?
Or do I make a patch for 3.0.5?
Apart from this functionality I am more than happy with everything else and thank you so much for all the hard work.
Sorry about this being a n00b question and if there is an FAQ or similar I have missed with this information then I apologise.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>