From d392259ee3e815ed3afe710b836b0bca60b95fd7 Mon Sep 17 00:00:00 2001 From: Alex Tomlins Date: Mon, 22 Jun 2009 16:51:02 +0100 Subject: [PATCH] Make has_many :through update counter_cache on join model correctly. --- .../associations/has_many_through_association.rb | 2 +- .../has_many_through_associations_test.rb | 8 ++++---- .../test/cases/associations/join_model_test.rb | 10 ++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) 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 1c091e7..01f5d77 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -64,7 +64,7 @@ module ActiveRecord def delete_records(records) klass = @reflection.through_reflection.klass records.each do |associate| - klass.delete_all(construct_join_attributes(associate)) + klass.destroy_all(construct_join_attributes(associate)) end 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 97efca7..5d507b7 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -81,7 +81,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_delete_association assert_queries(2){posts(:welcome);people(:michael); } - assert_queries(1) do + assert_queries(2) do posts(:welcome).people.delete(people(:michael)) end @@ -113,9 +113,9 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} - # 1 query to delete the existing reader (michael) + # 2 queries to destroy the existing reader (michael) # 1 query to associate the new reader (david) - assert_queries(2) do + assert_queries(3) do posts(:welcome).people = [people(:david)] end @@ -160,7 +160,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_clear_associations assert_queries(2) { posts(:welcome);posts(:welcome).people(true) } - assert_queries(1) do + assert_queries(2) do posts(:welcome).people.clear end diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index b1060d0..9314069 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -572,6 +572,16 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert_equal(tags_before.sort, post_thinking.tags.sort) end + def test_update_associate_counter_cache_when_deleting_from_has_many_through + assert_equal 0, posts(:welcome)[:taggings_count] + tag = Tag.create!(:name => 'doomed') + posts(:welcome).tags << tag + assert_equal 1, posts(:welcome, :reload)[:taggings_count] + + assert_nothing_raised { posts(:welcome).tags.delete(tag) } + assert posts(:welcome, :reload)[:taggings_count].zero? + end + def test_deleting_junk_from_has_many_through_should_raise_type_mismatch assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags.delete("Uhh what now?") } end -- 1.6.3.1