summaryrefslogtreecommitdiff
path: root/app/models/event.rb
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2018-01-03 17:44:29 +0100
committerYorick Peterse <yorickpeterse@gmail.com>2018-01-04 14:32:38 +0100
commitdac51ace521d7b2b2a5a5bb19167a8690ead242e (patch)
tree2e6d20697bdfe4cc5d71d4f62075a186867236dd /app/models/event.rb
parent1dac4271798a3b9ad36c3d985a3f7740cd1c60b3 (diff)
downloadgitlab-ce-dac51ace521d7b2b2a5a5bb19167a8690ead242e.tar.gz
Eager load event target authors whenever possibleconditionally-eager-load-event-target-authors
This ensures that the "author" association of an event's "target" association is eager loaded whenever the "target" association defines an "author" association. This in turn solves the N+1 query problem we first tried to solve in https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/15788 but caused problems when displaying milestones as those don't define an "author" association. The approach in this commit does mean that the authors are _always_ eager loaded since this takes place in the "belongs_to" block. This however shouldn't pose too much of a problem, and as far as I can tell there's no real way around this unfortunately.
Diffstat (limited to 'app/models/event.rb')
-rw-r--r--app/models/event.rb13
1 files changed, 12 insertions, 1 deletions
diff --git a/app/models/event.rb b/app/models/event.rb
index 0997b056c6a..8a79100de5a 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -48,7 +48,18 @@ class Event < ActiveRecord::Base
belongs_to :author, class_name: "User"
belongs_to :project
- belongs_to :target, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
+
+ belongs_to :target, -> {
+ # If the association for "target" defines an "author" association we want to
+ # eager-load this so Banzai & friends don't end up performing N+1 queries to
+ # get the authors of notes, issues, etc.
+ if reflections['events'].active_record.reflect_on_association(:author)
+ includes(:author)
+ else
+ self
+ end
+ }, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
+
has_one :push_event_payload
# Callbacks