From 7357e92865d001cd1631fd15a8512baa1248241c Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 30 Dec 2021 22:15:59 +0100 Subject: [PATCH] Eager load relationships for questions, answers and users --- app/controllers/answer_controller.rb | 2 +- app/controllers/discover_controller.rb | 8 ++++---- app/controllers/user_controller.rb | 4 ++-- app/models/user/answer_methods.rb | 1 + app/models/user/timeline_methods.rb | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/controllers/answer_controller.rb b/app/controllers/answer_controller.rb index e6abf19c..3e9bdd02 100644 --- a/app/controllers/answer_controller.rb +++ b/app/controllers/answer_controller.rb @@ -1,6 +1,6 @@ class AnswerController < ApplicationController def show - @answer = Answer.find(params[:id]) + @answer = Answer.find(params[:id]).include(comments: [:user], question: [:user]) @display_all = true if user_signed_in? diff --git a/app/controllers/discover_controller.rb b/app/controllers/discover_controller.rb index 3f7d0c41..7f415c78 100644 --- a/app/controllers/discover_controller.rb +++ b/app/controllers/discover_controller.rb @@ -8,10 +8,10 @@ class DiscoverController < ApplicationController top_x = 10 # only display the top X items - @popular_answers = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:smile_count).reverse_order.limit(top_x) - @most_discussed = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:comment_count).reverse_order.limit(top_x) - @popular_questions = Question.where("created_at > ?", Time.now.ago(1.week)).order(:answer_count).reverse_order.limit(top_x) - @new_users = User.where("asked_count > 0").order(:id).reverse_order.limit(top_x) + @popular_answers = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:smile_count).reverse_order.limit(top_x).includes(:question, :user, :comments) + @most_discussed = Answer.where("created_at > ?", Time.now.ago(1.week)).order(:comment_count).reverse_order.limit(top_x).includes(:question, :user, :comments) + @popular_questions = Question.where("created_at > ?", Time.now.ago(1.week)).order(:answer_count).reverse_order.limit(top_x).includes(:user) + @new_users = User.where("asked_count > 0").order(:id).reverse_order.limit(top_x).includes(:profile) # .user = the user # .question_count = how many questions did the user ask diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index cd34728e..a31bc566 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -4,7 +4,7 @@ class UserController < ApplicationController before_action :authenticate_user!, only: %w(edit update edit_privacy update_privacy edit_theme update_theme preview_theme delete_theme data export begin_export edit_security update_2fa destroy_2fa reset_user_recovery_codes) def show - @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first! + @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first! @answers = @user.cursored_answers(last_id: params[:last_id]) @answers_last_id = @answers.map(&:id).min @more_data_available = !@user.cursored_answers(last_id: @answers_last_id, size: 1).count.zero? @@ -99,7 +99,7 @@ class UserController < ApplicationController def friends @title = 'Following' @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).first! - @users = @user.cursored_friends(last_id: params[:last_id]) + @users = @user.cursored_friends(last_id: params[:last_id]).includes(:profile) @users_last_id = @users.map(&:id).min @more_data_available = !@user.cursored_friends(last_id: @users_last_id, size: 1).count.zero? @type = :friend diff --git a/app/models/user/answer_methods.rb b/app/models/user/answer_methods.rb index 3955a8cb..90200c4d 100644 --- a/app/models/user/answer_methods.rb +++ b/app/models/user/answer_methods.rb @@ -9,5 +9,6 @@ module User::AnswerMethods answers .order(:created_at) .reverse_order + .includes(comments: [:user], question: [:user]) end end diff --git a/app/models/user/timeline_methods.rb b/app/models/user/timeline_methods.rb index 364d756f..38b5e0d0 100644 --- a/app/models/user/timeline_methods.rb +++ b/app/models/user/timeline_methods.rb @@ -7,6 +7,6 @@ module User::TimelineMethods # @return [Array] the users' timeline def timeline - Answer.where('user_id in (?) OR user_id = ?', friend_ids, id).order(:created_at).reverse_order + Answer.where('user_id in (?) OR user_id = ?', friend_ids, id).order(:created_at).reverse_order.includes(comments: [:user], question: [:user], user: [:profile]) end end