Eager load relationships for questions, answers and users
This commit is contained in:
parent
5552f04bdf
commit
7357e92865
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -9,5 +9,6 @@ module User::AnswerMethods
|
|||
answers
|
||||
.order(:created_at)
|
||||
.reverse_order
|
||||
.includes(comments: [:user], question: [:user])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue