Permalink
Browse files

Merge pull request #12748 from dm1try/fix_counter_cache_for_through_a…

…ssociation

Fixes #11079, prevent the counter cache from being decremented twice when destroying a record on a has_many :through association
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
1 parent a64080f commit a54d229daca92ed1a117b3cc8398ef56feca6358 @senny senny committed Nov 11, 2013
@@ -1,3 +1,10 @@
+* Prevent the counter cache from being decremented twice when destroying
+ a record on a has_many :through association.
+
+ Fixes #11079.
+
+ *Dmitry Dedov*
+
* Fix bug where has_one associaton record update result in crash, when replaced with itself.
Fixes #12834.
@@ -149,7 +149,7 @@ def delete_records(records, method)
delete_through_records(records)
- if source_reflection.options[:counter_cache]
+ if source_reflection.options[:counter_cache] && method != :destroy
counter = source_reflection.counter_cache_column
klass.decrement_counter counter, records.map(&:id)
end
@@ -342,6 +342,15 @@ def test_update_counter_caches_on_replace_association
assert_equal(post.taggings.count, post.taggings_count)
end
+ def test_update_counter_caches_on_destroy
+ post = posts(:welcome)
+ tag = post.tags.create!(name: 'doomed')
+
+ assert_difference 'post.reload.taggings_count', -1 do
+ tag.tagged_posts.destroy(post)
+ end
+ end
+
def test_replace_association
assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}

0 comments on commit a54d229

Please sign in to comment.