Skip to content

Commit e4e472d

Browse files
committed
Fix eager loading in questions and answers
1 parent 5c769fb commit e4e472d

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

app/controllers/api/v2/plans_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class PlansController < BaseApiController # rubocop:todo Style/Documentation
1010
def show
1111
raise Pundit::NotAuthorizedError unless @scopes.include?('read')
1212

13-
@plan = Plan.find_by(id: params[:id])
13+
@plan = Plan.includes(roles: :user).find(params[:id])
1414

1515
raise Pundit::NotAuthorizedError unless @plan.present?
1616

app/presenters/api/v2/plan_presenter.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@ def plan_costs(plan:)
6060

6161
# Fetch all questions and answers from a plan, regardless of theme
6262
def fetch_all_q_and_a
63-
return [] unless @plan.questions.present?
63+
answers = @plan.answers.includes(:question)
64+
return [] unless answers.present?
6465

65-
@plan.questions.filter_map do |q|
66-
a = @plan.answers.find { |ans| ans.question_id == q.id }
67-
next unless a.present?
66+
answers.filter_map do |answer|
67+
q = answer.question
68+
next unless q.present?
6869

6970
{
7071
title: "Question #{q.number || q.id}",
7172
question: q.text.to_s,
72-
answer: a.text.to_s
73+
answer: answer.text.to_s
7374
}
7475
end
7576
end

0 commit comments

Comments
 (0)