aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-07-24 13:31:28 -0400
committerGitHub <noreply@github.com>2017-07-24 13:31:28 -0400
commitdd58cc2ebb9cc0f4f4db50a1a6d3296ad9a6a8e2 (patch)
treec010db36f87f3cd3720c53877c158b8549e408cb /activerecord
parent501941b10828f274135ec974814ed815e4ffcffa (diff)
parent6137a4e8f77f0bba78d304d962b89ddf3cf465aa (diff)
downloadrails-dd58cc2ebb9cc0f4f4db50a1a6d3296ad9a6a8e2.tar.gz
rails-dd58cc2ebb9cc0f4f4db50a1a6d3296ad9a6a8e2.tar.bz2
rails-dd58cc2ebb9cc0f4f4db50a1a6d3296ad9a6a8e2.zip
Merge pull request #29765 from lugray/fix_counter_cache
Fix `counter_cache` double increment
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb4
-rw-r--r--activerecord/lib/active_record/counter_cache.rb1
-rw-r--r--activerecord/test/cases/associations/belongs_to_associations_test.rb11
-rw-r--r--activerecord/test/models/comment.rb6
4 files changed, 18 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 7b303e923b..9904ee4bed 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -34,9 +34,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
foreign_key = reflection.foreign_key
cache_column = reflection.counter_cache_column
- if (@_after_create_counter_called ||= false)
- @_after_create_counter_called = false
- elsif (@_after_replace_counter_called ||= false)
+ if (@_after_replace_counter_called ||= false)
@_after_replace_counter_called = false
elsif saved_change_to_attribute?(foreign_key) && !new_record?
if reflection.polymorphic?
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index ab2bfb63a6..5005d58f1c 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -182,7 +182,6 @@ module ActiveRecord
each_counter_cached_associations do |association|
if send(association.reflection.name)
association.increment_counters
- @_after_create_counter_called = true
end
end
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb
index 7e4ded9fc9..4f9d5ee3fa 100644
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -1171,6 +1171,17 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
Column.create! record: record
assert_equal 1, Column.count
end
+
+ def test_multiple_counter_cache_with_after_create_update
+ post = posts(:welcome)
+ parent = comments(:greetings)
+
+ assert_difference "parent.reload.children_count", +1 do
+ assert_difference "post.reload.comments_count", +1 do
+ comment = CommentWithAfterCreateUpdate.create(body: "foo", post: post, parent: parent)
+ end
+ end
+ end
end
class BelongsToWithForeignKeyTest < ActiveRecord::TestCase
diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb
index e1e2a5e0e9..61c54a77a7 100644
--- a/activerecord/test/models/comment.rb
+++ b/activerecord/test/models/comment.rb
@@ -77,3 +77,9 @@ class CommentWithDefaultScopeReferencesAssociation < Comment
default_scope -> { includes(:developer).order("developers.name").references(:developer) }
belongs_to :developer
end
+
+class CommentWithAfterCreateUpdate < Comment
+ after_create do
+ update_attributes(body: "bar")
+ end
+end