1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove force parent loading when counter cache child is created/destroyed

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

Related 52e11e462f.
This commit is contained in:
Ryuta Kamizono 2018-09-26 23:37:09 +09:00
parent 52e11e462f
commit a592e87a27
2 changed files with 7 additions and 7 deletions

View file

@ -163,9 +163,7 @@ module ActiveRecord
id = super
each_counter_cached_associations do |association|
if send(association.reflection.name)
association.increment_counters
end
association.increment_counters
end
id
@ -178,9 +176,7 @@ module ActiveRecord
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

View file

@ -578,7 +578,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
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!