From 491fb359d223276f7f51cf728150ac2efe36f6dc Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 22 Oct 2023 19:51:23 +0200 Subject: [PATCH] Ensure user profiles and questions are eager loaded --- app/controllers/user_controller.rb | 2 +- app/models/answer/timeline_methods.rb | 2 +- app/models/list/timeline_methods.rb | 3 ++- app/models/question/answer_methods.rb | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 7b9084b9..7a7be8ef 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -8,7 +8,7 @@ class UserController < ApplicationController after_action :mark_notification_as_read, only: %i[show] def show - @pinned_answers = @user.answers.pinned.order(pinned_at: :desc).limit(10) + @pinned_answers = @user.answers.pinned.includes([{ user: :profile }, :question]).order(pinned_at: :desc).limit(10) paginate_answers { |args| @user.cursored_answers(**args) } respond_to do |format| diff --git a/app/models/answer/timeline_methods.rb b/app/models/answer/timeline_methods.rb index 8c6085bf..3245aa7e 100644 --- a/app/models/answer/timeline_methods.rb +++ b/app/models/answer/timeline_methods.rb @@ -6,7 +6,7 @@ module Answer::TimelineMethods define_cursor_paginator :cursored_public_timeline, :public_timeline def public_timeline(current_user: nil) - joins(:user) + includes([{ user: :profile }, :question]) .then do |query| next query unless current_user diff --git a/app/models/list/timeline_methods.rb b/app/models/list/timeline_methods.rb index 7909d544..b02d87e3 100644 --- a/app/models/list/timeline_methods.rb +++ b/app/models/list/timeline_methods.rb @@ -5,9 +5,10 @@ module List::TimelineMethods define_cursor_paginator :cursored_timeline, :timeline - # @return [Array] the lists' timeline + # @return [ActiveRecord::Relation] the lists' timeline def timeline(current_user: nil) Answer + .includes([{ user: :profile }, :question]) .then do |query| next query unless current_user diff --git a/app/models/question/answer_methods.rb b/app/models/question/answer_methods.rb index efb94a7b..1a01dc7b 100644 --- a/app/models/question/answer_methods.rb +++ b/app/models/question/answer_methods.rb @@ -7,6 +7,7 @@ module Question::AnswerMethods def ordered_answers(current_user: nil) answers + .includes([{ user: :profile }, :question]) .then do |query| next query unless current_user