From b8bfe50a5080909c7ba2b517d99d3c5f79d2a32f Mon Sep 17 00:00:00 2001
From: Ahmad Sherif <me@ahmadsherif.com>
Date: Tue, 20 Sep 2016 23:05:49 +0200
Subject: [PATCH] Reduce number of queries when calling
 GlobalMilestone#{labels,participants}

---
 CHANGELOG                      |  1 +
 app/models/global_milestone.rb | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 5fbc8830d7b..90092c3008e 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -180,6 +180,7 @@ v 8.12.0 (unreleased)
   - Clean environment variables when running git hooks
   - Fix Import/Export issues importing protected branches and some specific models
   - Fix non-master branch readme display in tree view
+  - Speed-up group milestones show page
   - Add UX improvements for merge request version diffs
 
 v 8.11.7
diff --git a/app/models/global_milestone.rb b/app/models/global_milestone.rb
index 81b7343d7ad..8f38a2b6254 100644
--- a/app/models/global_milestone.rb
+++ b/app/models/global_milestone.rb
@@ -61,11 +61,11 @@ class GlobalMilestone
   end
 
   def participants
-    @participants ||= milestones.map(&:participants).flatten.compact.uniq
+    @participants ||= milestones_relation.includes(:participants).map(&:participants).flatten.compact.uniq
   end
 
   def labels
-    @labels ||= GlobalLabel.build_collection(milestones.map(&:labels).flatten)
+    @labels ||= GlobalLabel.build_collection(milestones_relation.includes(:labels).map(&:labels).flatten)
                            .sort_by!(&:title)
   end
 
@@ -89,4 +89,14 @@ class GlobalMilestone
       end
     end
   end
+
+  private
+
+  def milestones_relation
+    @milestones_relation ||= if milestones.is_a?(ActiveRecord::Relation)
+                               milestones
+                             else
+                               Milestone.where(id: milestones.map(&:id))
+                             end
+  end
 end
-- 
GitLab