Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13018 from heruku/scoping_fix

changed update counter to act on unscoped model
Conflicts:
	activerecord/CHANGELOG.md
	activerecord/test/cases/associations/has_many_associations_test.rb
  • Loading branch information...
commit bd4abae4ac1a0a39cd31a4a4d71cce1c3eaade7f 1 parent 34e6094
@senny senny authored
View
6 activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Update counter cache on a has_many relationship regardless of default scope
+
+ Fix #12952.
+
+ *Uku Taht*
+
* Fix bug when validating the uniqueness of an aliased attribute.
Fixes #12402.
View
2  activerecord/lib/active_record/counter_cache.rb
@@ -77,7 +77,7 @@ def update_counters(id, counters)
"#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
end
- where(primary_key => id).update_all updates.join(', ')
+ unscoped.where(primary_key => id).update_all updates.join(', ')
end
# Increment a numeric field by one, via a direct SQL update.
View
8 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1851,4 +1851,12 @@ def test_collection_association_with_private_kernel_method
assert_equal 1, speedometer.minivans.to_a.size, "Only one association should be present:\n#{speedometer.minivans.to_a}"
assert_equal 1, speedometer.reload.minivans.to_a.size
end
+
+ test 'updates counter cache when default scope is given' do
+ topic = DefaultRejectedTopic.create approved: true
+
+ assert_difference "topic.reload.replies_count", 1 do
+ topic.approved_replies.create!
+ end
+ end
end
View
4 activerecord/test/models/topic.rb
@@ -107,6 +107,10 @@ class ImportantTopic < Topic
serialize :important, Hash
end
+class DefaultRejectedTopic < Topic
+ default_scope -> { where(approved: false) }
+end
+
class BlankTopic < Topic
# declared here to make sure that dynamic finder with a bang can find a model that responds to `blank?`
def blank?
Please sign in to comment.
Something went wrong with that request. Please try again.