This project is archived and is in readonly mode.
counter_cache not decrementing on delete
Reported by Robert Sosinski | October 9th, 2008 @ 05:28 AM | in 2.1.3
On Rails 2.1.1, the counter for a model that uses counter_cache increments with doing:
@post.comments.push(comment)
However, will not decrement with:
@post.comments.delete(comment)
Comments and changes to this ticket
-
Kai Krakow November 28th, 2008 @ 02:30 PM
This is especially annoying on has_many :through associations (given these associations):
class Tag; belongs_to :post, :counter_cache => true; belongs_to :tag_name; end class TagNames; ...; end class Post; has_many :tags; has_many :tag_names, :through => :tags; end
When a form submission directly assigns to @post.tag_names the counter get's increased correctly, but never decreases if one removes tags. Or clears all.
-
Emilio Tagua December 2nd, 2008 @ 05:13 PM
Here is the patch to fix this, tests for has many and HMT included.
-
Jeremy Kemper December 10th, 2008 @ 07:39 PM
- Tag changed from activerecord, counter_cache to activerecord, counter_cache, patch
- State changed from new to committed
- Milestone changed from 2.x to 2.1.3
-
Jeremy Kemper December 10th, 2008 @ 07:47 PM
- Assigned user set to Jeremy Kemper
- State changed from committed to open
This causes two failing tests on sqlite3.
-
Jeremy Kemper December 10th, 2008 @ 10:47 PM
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 1f8b297..d97f6f3 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -553,15 +553,16 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_deleting_updates_counter_cache - post = Post.first + topic = Topic.first + assert_equal topic.replies.to_a.size, topic.replies_count - post.comments.delete(post.comments.first) - post.reload - assert_equal post.comments(true).size, post.comments_count + topic.replies.delete(topic.replies.first) + topic.reload + assert_equal topic.replies.to_a.size, topic.replies_count - post.comments.delete(post.comments.first) - post.reload - assert_equal 0, post.comments_count + topic.replies.delete(topic.replies.first) + topic.reload + assert_equal topic.replies.to_a.size, topic.replies_count end def test_deleting_before_save @@ -618,11 +619,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_clearing_updates_counter_cache - post = Post.first + topic = Topic.first - post.comments.clear - post.reload - assert_equal 0, post.comments_count + topic.replies.clear + topic.reload + assert_equal 0, topic.replies_count end def test_clearing_a_dependent_association_collection
Because comments.post_id is not null. This switches it to use topic + replies, but the test still fails. I'm reverting the original commit until this is resolved.
-
Repository December 10th, 2008 @ 10:50 PM
(from [5b290082cf91e130af4da989d6bafc69d8f2607a]) Revert "Fix: counter_cache should decrement on deleting associated records."
[#1196 state:open]
This reverts commit 757e4364dc3f808f0002a6c8cb03531e69e2f356. http://github.com/rails/rails/co...
-
Repository December 10th, 2008 @ 10:50 PM
(from [8fcd9cfb9f4e6856df3349a8235621768a565e17]) Revert "Fix: counter_cache should decrement on deleting associated records."
[#1196 state:open]
This reverts commit c9e176de78067c5941233b12686276d3016fbb38. http://github.com/rails/rails/co...
-
Repository December 10th, 2008 @ 10:53 PM
(from [b30ae1974851b20ef430df9de17e6e79e5b25ad2]) Revert "Fix: counter_cache should decrement on deleting associated records."
[#1196 state:open]
This reverts commit 05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc. http://github.com/rails/rails/co...
-
Jeremy Kemper January 10th, 2009 @ 08:07 PM
- State changed from open to stale
-
CancelProfileIsBroken August 4th, 2009 @ 05:39 PM
- Tag changed from activerecord, counter_cache, patch to activerecord, bugmash, counter_cache, patch
-
Gabe da Silveira August 9th, 2009 @ 09:21 PM
Rebased the test cases against 2-3-stable and verified that they do indeed fail.
I've attached a test.
-
Gabe da Silveira August 10th, 2009 @ 12:20 AM
Okay, after integrating bitsweat's test changes it was revealed that the actual problem footprint is much smaller. (The original patch conflated a buggy test with a buggy fix). What I came up with is that counter_caches were broken when the association was deleted by nullifying the foreign key rather than destroying the record. I refactored and stripped down the test that shows this and moved it into the has_many test rather than has_many :through test_deleting_updates_counter_cache_without_dependent_destroy. I left the other two tests in place for posterity though they do not fail before the fix.
The actual fix was a one-liner.
I've attached a patch.
-
Elad Meidar August 10th, 2009 @ 12:30 AM
+1 verified, applied cleanly on master and 2-3-stable, all tests pass.
-
Rizwan Reza August 10th, 2009 @ 01:34 AM
verified
+1 The patch works cleanly. All tests are passing as well.
-
CancelProfileIsBroken August 10th, 2009 @ 02:23 AM
- State changed from stale to open
-
Repository August 10th, 2009 @ 05:32 AM
- State changed from open to committed
(from [9bc80f4dd149ee70aa05c352bdd3fec46d22871a]) Fix that counter_cache breaks with has_many :dependent => :nullify.
[#1196 state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/9bc80f4dd149ee70aa05c352bdd3fe... -
Repository August 10th, 2009 @ 05:32 AM
(from [7e3364ac4634f7017305c4bc725710ab0e7ba4c2]) Fix that counter_cache breaks with has_many :dependent => :nullify.
[#1196 state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/7e3364ac4634f7017305c4bc725710... -
Jeremy Kemper August 10th, 2009 @ 05:40 AM
- Tag changed from activerecord, bugmash, counter_cache, patch to activerecord, counter_cache, patch
-
Ryan Bigg October 9th, 2010 @ 10:01 PM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
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>
People watching this ticket
Attachments
Referenced by
- 1196 counter_cache not decrementing on delete [#1196 state:committed]
- 1196 counter_cache not decrementing on delete [#1196 state:committed]
- 1196 counter_cache not decrementing on delete [#1196 state:open]
- 1196 counter_cache not decrementing on delete [#1196 state:open]
- 1196 counter_cache not decrementing on delete [#1196 state:open]
- 1843 Calling delete on a has_many :through association does not call destroy on the association object See also: #1196, http://railsforum.com/viewtopic....