From dd31a7092f390e8b511643e47ac992cf9056d0f1 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 7b9084b99..f8fce9d00 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 8c6085bf4..c46ad0e45 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 7909d544a..1421f048c 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 efb94a7be..1fc4dd243 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