aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-04-30 22:48:50 -0600
committerSean Griffin <sean@seantheprogrammer.com>2016-04-30 22:48:50 -0600
commita4b3c78e8619ba84625ceb83118915ef36ec2cfc (patch)
treefbcc6449a23ed12deeb668959b75fa3706b12673 /activerecord/lib
parent0725f289e1bf70e1874de5fa7ed3312b410085f2 (diff)
parentc3e3577f9d5058382504773bf0d32afa15cb131e (diff)
downloadrails-a4b3c78e8619ba84625ceb83118915ef36ec2cfc.tar.gz
rails-a4b3c78e8619ba84625ceb83118915ef36ec2cfc.tar.bz2
rails-a4b3c78e8619ba84625ceb83118915ef36ec2cfc.zip
Merge pull request #24713 from tomkadwill/fix_counter_cache_increment
Fix counter_cache double increment bug Close #24183
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb1
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb2
2 files changed, 3 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index 41698c5360..24997370b2 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -61,6 +61,7 @@ module ActiveRecord
def update_counters_on_replace(record)
if require_counter_update? && different_target?(record)
+ owner.instance_variable_set :@_after_replace_counter_called, true
record.increment!(reflection.counter_cache_column)
decrement_counters
end
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 346329c610..3121e70a04 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -33,6 +33,8 @@ module ActiveRecord::Associations::Builder # :nodoc:
if (@_after_create_counter_called ||= false)
@_after_create_counter_called = false
+ elsif (@_after_replace_counter_called ||= false)
+ @_after_replace_counter_called = false
elsif attribute_changed?(foreign_key) && !new_record?
if reflection.polymorphic?
model = attribute(reflection.foreign_type).try(:constantize)