提交 6636df3b 编写于 作者: R Rafael Mendonça França

Merge pull request #16028 from cade/fix_counter_cache_count_with_association_selects

Add `:all` argument to `count` in `reset_counters`

Conflicts:
	activerecord/CHANGELOG.md
* Fixed error in `reset_counters` when associations have `select` scope.
(Call to `count` generates invalid SQL.)
*Cade Truitt*
* After a successful `reload`, `new_record?` is always false.
Fixes #12101.
......
......@@ -38,7 +38,7 @@ def reset_counters(id, *counters)
counter_name = reflection.counter_cache_column
stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({
arel_table[counter_name] => object.send(counter_association).count
arel_table[counter_name] => object.send(counter_association).count(:all)
}, primary_key)
connection.update stmt
end
......
......@@ -19,6 +19,7 @@ class CounterCacheTest < ActiveRecord::TestCase
class ::SpecialTopic < ::Topic
has_many :special_replies, :foreign_key => 'parent_id'
has_many :lightweight_special_replies, -> { select('topics.id, topics.title') }, :foreign_key => 'parent_id', :class_name => 'SpecialReply'
end
class ::SpecialReply < ::Reply
......@@ -170,4 +171,13 @@ class ::SpecialReply < ::Reply
end
assert_equal "'Topic' has no association called 'undefined_count'", e.message
end
test "reset counter works with select declared on association" do
special = SpecialTopic.create!(:title => 'Special')
SpecialTopic.increment_counter(:replies_count, special.id)
assert_difference 'special.reload.replies_count', -1 do
SpecialTopic.reset_counters(special.id, :lightweight_special_replies)
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册