提交 d3567824 编写于 作者: Y Yves Senn

Merge pull request #13018 from heruku/scoping_fix

changed update counter to act on unscoped model
* Update counter cache on a has_many relationship regardless of default scope
Fix #12952.
*Uku Taht*
* `rename_index` adds the new index before removing the old one. This allows
to rename indexes on columns with a foreign key and prevents the following error:
......
......@@ -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.
......
......@@ -1781,4 +1781,12 @@ def test_collection_association_with_private_kernel_method
assert_equal [original_child], car.reload.failed_bulbs
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
......@@ -106,6 +106,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?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册