Skip to content

Commit

Permalink
Remove force parent loading when counter cache child is created/destr…
Browse files Browse the repository at this point in the history
…oyed

`association.increment_counters` and `association.decrement_counters`
works regardless of parent target is loaded or not.

Related 52e11e4.
  • Loading branch information
kamipo committed Sep 26, 2018
1 parent 52e11e4 commit a592e87
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 2 additions & 6 deletions activerecord/lib/active_record/counter_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ def _create_record(attribute_names = self.attribute_names)
id = super

each_counter_cached_associations do |association|
if send(association.reflection.name)
association.increment_counters
end
association.increment_counters
end

id
Expand All @@ -178,9 +176,7 @@ def destroy_row
each_counter_cached_associations do |association|
foreign_key = association.reflection.foreign_key.to_sym
unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
if send(association.reflection.name)
association.decrement_counters
end
association.decrement_counters
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,11 @@ def test_belongs_to_reassign_with_namespaced_models_and_counters

def test_belongs_to_counter_after_save
topic = Topic.create!(title: "monday night")
topic.replies.create!(title: "re: monday night", content: "football")

assert_queries(2) do
topic.replies.create!(title: "re: monday night", content: "football")
end

assert_equal 1, Topic.find(topic.id)[:replies_count]

topic.save!
Expand Down

0 comments on commit a592e87

Please sign in to comment.