Skip to content

Commit

Permalink
has_many associations with :dependent => :delete_all should update th…
Browse files Browse the repository at this point in the history
…e counter cache when deleting records
  • Loading branch information
jonleighton authored and tenderlove committed Dec 20, 2010
1 parent 57b9009 commit 834e533
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
Expand Up @@ -72,8 +72,10 @@ def delete_records(records)
with_scope(@scope) do with_scope(@scope) do
@reflection.klass.update_all(updates, conditions) @reflection.klass.update_all(updates, conditions)
end end
end


@owner.class.update_counters(@owner.id, cached_counter_attribute_name => -records.size) if has_cached_counter? if has_cached_counter? && @reflection.options[:dependent] != :destroy
@owner.class.update_counters(@owner.id, cached_counter_attribute_name => -records.size)
end end
end end


Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -616,6 +616,18 @@ def test_deleting_updates_counter_cache_without_dependent_destroy
end end
end end


def test_deleting_updates_counter_cache_with_dependent_delete_all
post = posts(:welcome)

# Manually update the count as the tagging will have been added to the taggings association,
# rather than to the taggings_with_delete_all one (which is just a 'shadow' of the former)
post.update_attribute(:taggings_with_delete_all_count, post.taggings_with_delete_all.to_a.count)

assert_difference "post.reload.taggings_with_delete_all_count", -1 do
post.taggings_with_delete_all.delete(post.taggings_with_delete_all.first)
end
end

def test_deleting_a_collection def test_deleting_a_collection
force_signal37_to_load_all_clients_of_firm force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.create("name" => "Another Client") companies(:first_firm).clients_of_firm.create("name" => "Another Client")
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -59,6 +59,8 @@ def add_joins_and_select
end end
end end


has_many :taggings_with_delete_all, :class_name => 'Tagging', :as => :taggable, :dependent => :delete_all

has_many :misc_tags, :through => :taggings, :source => :tag, :conditions => "tags.name = 'Misc'" has_many :misc_tags, :through => :taggings, :source => :tag, :conditions => "tags.name = 'Misc'"
has_many :funky_tags, :through => :taggings, :source => :tag has_many :funky_tags, :through => :taggings, :source => :tag
has_many :super_tags, :through => :taggings has_many :super_tags, :through => :taggings
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -441,6 +441,7 @@ def create_table(*args, &block)
t.string :type t.string :type
t.integer :comments_count, :default => 0 t.integer :comments_count, :default => 0
t.integer :taggings_count, :default => 0 t.integer :taggings_count, :default => 0
t.integer :taggings_with_delete_all_count, :default => 0
end end


create_table :price_estimates, :force => true do |t| create_table :price_estimates, :force => true do |t|
Expand Down

0 comments on commit 834e533

Please sign in to comment.