From 8881e56df592510f023521723b94ccd846c5511d Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 17 Nov 2014 18:02:17 +1100 Subject: [PATCH] PERF: don't ever use inject on AR relations inject will deteriorate to a method missing that invokes arel and a world of uneeded work --- app/serializers/category_detailed_serializer.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/serializers/category_detailed_serializer.rb b/app/serializers/category_detailed_serializer.rb index 531c01435..b1d68d1be 100644 --- a/app/serializers/category_detailed_serializer.rb +++ b/app/serializers/category_detailed_serializer.rb @@ -72,7 +72,11 @@ class CategoryDetailedSerializer < BasicCategorySerializer end def count_with_subcategories(method) - object.subcategories.inject(object.send(method) || 0) { |sum,c| sum += (c.send(method) || 0) } + count = object.send(method) || 0 + object.subcategories.each do |category| + count += (object.send(method) || 0) + end + count end end