diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-01-03 19:44:29 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-01-04 16:32:38 +0300 |
commit | dac51ace521d7b2b2a5a5bb19167a8690ead242e (patch) | |
tree | 2e6d20697bdfe4cc5d71d4f62075a186867236dd /app/models/event.rb | |
parent | 1dac4271798a3b9ad36c3d985a3f7740cd1c60b3 (diff) |
Eager load event target authors whenever possible
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.rb | 13 |
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 |