Eager load profiles in paginators

This commit is contained in:
Karina Kwiatek 2023-01-28 21:17:08 +01:00
parent 4dcc5dea55
commit 32ba17ac72
3 changed files with 10 additions and 4 deletions

View file

@ -5,10 +5,11 @@ module User::AnswerMethods
define_cursor_paginator :cursored_answers, :ordered_answers
# @return [ActiveRecord::Relation<Answer>] List of a user's answers
def ordered_answers
answers
.order(:created_at)
.reverse_order
.includes(comments: [:user, :smiles], question: [:user], smiles: [:user])
.includes(comments: %i[user smiles], question: { user: :profile }, smiles: [:user])
end
end

View file

@ -5,9 +5,10 @@ module User::InboxMethods
define_cursor_paginator :cursored_inbox, :ordered_inbox
# @return [ActiveRecord::Relation<Inbox>] the user's inbox entries
def ordered_inbox
inboxes
.includes(:question, :user)
.includes(:question, user: :profile)
.order(:created_at)
.reverse_order
end

View file

@ -5,8 +5,12 @@ module User::TimelineMethods
define_cursor_paginator :cursored_timeline, :timeline
# @return [Array] the users' timeline
# @return [ActiveRecord::Relation<Answer>] the user's timeline
def timeline
Answer.where("user_id in (?) OR user_id = ?", following_ids, id).order(:created_at).reverse_order.includes(comments: %i[user smiles], question: [:user], user: [:profile], smiles: [:user])
Answer
.where("user_id in (?) OR user_id = ?", following_ids, id)
.order(:created_at)
.reverse_order
.includes(comments: %i[user smiles], question: { user: :profile }, user: [:profile], smiles: [:user])
end
end