aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-12-14 10:21:44 -0800
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-12-14 10:21:44 -0800
commit0b97ffd45df207ececf139352cbcc8836af89b4c (patch)
treec1a42950d1312335bed4e5d1391d2eac009b6bbf
parent05e9075541f435b579a96a2519ab71ec27ace895 (diff)
parenta765c8426fe39c73cf9f3ad7f8edcf588c01a11d (diff)
downloadrails-0b97ffd45df207ececf139352cbcc8836af89b4c.tar.gz
rails-0b97ffd45df207ececf139352cbcc8836af89b4c.tar.bz2
rails-0b97ffd45df207ececf139352cbcc8836af89b4c.zip
Merge pull request #8400 from matthewrobertson/has-many-through-counter-cache
Fix for has_many_through counter_cache bug Counter caches were not being updated properly when replacing has_many_through relationships.
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/activerecord-4.0.0.beta.gembin0 -> 306688 bytes
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb5
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb11
4 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index de386cd358..be3b6564b0 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Fix counter cache columns not updated when replacing `has_many :through`
+ associations.
+
+ *Matthew Robertson*
+
* Recognize migrations placed in directories containing numbers and 'rb'.
Fix #8492
diff --git a/activerecord/activerecord-4.0.0.beta.gem b/activerecord/activerecord-4.0.0.beta.gem
new file mode 100644
index 0000000000..1309c478b8
--- /dev/null
+++ b/activerecord/activerecord-4.0.0.beta.gem
Binary files differ
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index c7d8a84a7e..c3266f2bb4 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -153,6 +153,11 @@ module ActiveRecord
delete_through_records(records)
+ if source_reflection.options[:counter_cache]
+ counter = source_reflection.counter_cache_column
+ klass.decrement_counter counter, records.map(&:id)
+ end
+
if through_reflection.macro == :has_many && update_through_counter?(method)
update_counter(-count, through_reflection)
end
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 8e52ce1d91..2b96b42032 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -330,6 +330,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_update_counter_caches_on_replace_association
+ post = posts(:welcome)
+ tag = post.tags.create!(:name => 'doomed')
+ tag.tagged_posts << posts(:thinking)
+
+ tag.tagged_posts = []
+ post.reload
+
+ assert_equal(post.taggings.count, post.taggings_count)
+ end
+
def test_replace_association
assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}