mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Update counter cache in memory if parent target is existed
Fixes #19550.
This commit is contained in:
parent
29c23cc7bd
commit
38fae1f250
2 changed files with 35 additions and 10 deletions
|
@ -31,24 +31,17 @@ module ActiveRecord::Associations::Builder # :nodoc:
|
||||||
|
|
||||||
mixin.class_eval do
|
mixin.class_eval do
|
||||||
def belongs_to_counter_cache_after_update(reflection)
|
def belongs_to_counter_cache_after_update(reflection)
|
||||||
foreign_key = reflection.foreign_key
|
|
||||||
cache_column = reflection.counter_cache_column
|
|
||||||
|
|
||||||
if association(reflection.name).target_changed?
|
if association(reflection.name).target_changed?
|
||||||
if reflection.polymorphic?
|
if reflection.polymorphic?
|
||||||
model = attribute_in_database(reflection.foreign_type).try(:constantize)
|
|
||||||
model_was = attribute_before_last_save(reflection.foreign_type).try(:constantize)
|
model_was = attribute_before_last_save(reflection.foreign_type).try(:constantize)
|
||||||
else
|
else
|
||||||
model = reflection.klass
|
|
||||||
model_was = reflection.klass
|
model_was = reflection.klass
|
||||||
end
|
end
|
||||||
|
|
||||||
foreign_key_was = attribute_before_last_save foreign_key
|
foreign_key_was = attribute_before_last_save(reflection.foreign_key)
|
||||||
foreign_key = attribute_in_database foreign_key
|
cache_column = reflection.counter_cache_column
|
||||||
|
|
||||||
if foreign_key && model < ActiveRecord::Base
|
association(reflection.name).increment_counters
|
||||||
counter_cache_target(reflection, model, foreign_key).update_counters(cache_column => 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
if foreign_key_was && model_was < ActiveRecord::Base
|
if foreign_key_was && model_was < ActiveRecord::Base
|
||||||
counter_cache_target(reflection, model_was, foreign_key_was).update_counters(cache_column => -1)
|
counter_cache_target(reflection, model_was, foreign_key_was).update_counters(cache_column => -1)
|
||||||
|
|
|
@ -1197,6 +1197,38 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_equal 2, topic.reload.replies.size
|
assert_equal 2, topic.reload.replies.size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_counter_cache_updates_in_memory_after_update_with_inverse_of_disabled
|
||||||
|
topic = Topic.create!(title: "Zoom-zoom-zoom")
|
||||||
|
|
||||||
|
assert_equal 0, topic.replies_count
|
||||||
|
|
||||||
|
reply1 = Reply.create!(title: "re: zoom", content: "speedy quick!")
|
||||||
|
reply2 = Reply.create!(title: "re: zoom 2", content: "OMG lol!")
|
||||||
|
|
||||||
|
assert_queries(6) do
|
||||||
|
topic.replies << [reply1, reply2]
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 2, topic.replies_count
|
||||||
|
assert_equal 2, topic.reload.replies_count
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_counter_cache_updates_in_memory_after_update_with_inverse_of_enabled
|
||||||
|
category = Category.create!(name: "Counter Cache")
|
||||||
|
|
||||||
|
assert_nil category.categorizations_count
|
||||||
|
|
||||||
|
categorization1 = Categorization.create!
|
||||||
|
categorization2 = Categorization.create!
|
||||||
|
|
||||||
|
assert_queries(4) do
|
||||||
|
category.categorizations << [categorization1, categorization2]
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 2, category.categorizations_count
|
||||||
|
assert_equal 2, category.reload.categorizations_count
|
||||||
|
end
|
||||||
|
|
||||||
def test_pushing_association_updates_counter_cache
|
def test_pushing_association_updates_counter_cache
|
||||||
topic = Topic.order("id ASC").first
|
topic = Topic.order("id ASC").first
|
||||||
reply = Reply.create!
|
reply = Reply.create!
|
||||||
|
|
Loading…
Reference in a new issue