Skip resetting correct counter caches
This commit is contained in:
parent
e20e541ff3
commit
fb1da7a3f5
@ -48,7 +48,9 @@ def reset_counters(id, *counters, touch: nil)
|
||||
reflection = child_class._reflections.values.find { |e| e.belongs_to? && e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
|
||||
counter_name = reflection.counter_cache_column
|
||||
|
||||
updates[counter_name] = object.send(counter_association).count(:all)
|
||||
count_was = object.send(counter_name)
|
||||
count = object.send(counter_association).count(:all)
|
||||
updates[counter_name] = count if count != count_was
|
||||
end
|
||||
|
||||
if touch
|
||||
@ -59,7 +61,7 @@ def reset_counters(id, *counters, touch: nil)
|
||||
updates.merge!(touch_updates)
|
||||
end
|
||||
|
||||
unscoped.where(primary_key => object.id).update_all(updates)
|
||||
unscoped.where(primary_key => object.id).update_all(updates) if updates.any?
|
||||
|
||||
true
|
||||
end
|
||||
|
@ -114,6 +114,27 @@ class ::SpecialReply < ::Reply
|
||||
end
|
||||
end
|
||||
|
||||
test "reset counter skips query for correct counter" do
|
||||
Topic.reset_counters(@topic.id, :replies_count)
|
||||
|
||||
# SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT ?
|
||||
# SELECT COUNT(*) FROM "topics" WHERE "topics"."type" IN (?, ?, ?, ?, ?) AND "topics"."parent_id" = ?
|
||||
assert_queries(2) do
|
||||
Topic.reset_counters(@topic.id, :replies_count)
|
||||
end
|
||||
end
|
||||
|
||||
test "reset counter performs query for correct counter with touch: true" do
|
||||
Topic.reset_counters(@topic.id, :replies_count)
|
||||
|
||||
# SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT ?
|
||||
# SELECT COUNT(*) FROM "topics" WHERE "topics"."type" IN (?, ?, ?, ?, ?) AND "topics"."parent_id" = ?
|
||||
# UPDATE "topics" SET "updated_at" = ? WHERE "topics"."id" = ?
|
||||
assert_queries(3) do
|
||||
Topic.reset_counters(@topic.id, :replies_count, touch: true)
|
||||
end
|
||||
end
|
||||
|
||||
test "update counter with initial null value" do
|
||||
category = categories(:general)
|
||||
assert_equal 2, category.categorizations.count
|
||||
|
Loading…
Reference in New Issue
Block a user