Skip to content

Commit 3534ee0

Browse files
committed
Replace iterated answer .destroy with .destroy_all
-This change maintains the functional logic of the deletion of answers (based on questions to be removed) while also optimising its efficiency.
1 parent bed03ec commit 3534ee0

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

app/controllers/answers_controller.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class AnswersController < ApplicationController
1212
# `remote: true` in the <form> tag and just send back the ERB.
1313
# Consider using ActionCable for the progress bar(s)
1414
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
15-
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
1615
def create_or_update
1716
p_params = permitted_params
1817

@@ -48,17 +47,11 @@ def create_or_update
4847
@question = @answer.question
4948
@section = @plan.sections.find_by(id: @question.section_id)
5049
template = @section.phase.template
51-
52-
# Get list of questions to be removed from the plan based on any conditional questions.
53-
questions_remove_list_before_destroying_answers = remove_list(@plan)
5450
all_question_ids = @plan.questions.pluck(:id)
5551

5652
# Destroy all answers for removed questions
57-
questions_remove_list_before_destroying_answers.each do |id|
58-
Answer.where(question_id: id, plan: @plan).each do |a|
59-
Answer.destroy(a.id)
60-
end
61-
end
53+
# - remove_list(@plan) returns a list of question to be removed from the plan based on any conditional questions.
54+
Answer.where(question_id: remove_list(@plan), plan: @plan).destroy_all
6255
# Now update @plan after removing answers of questions removed from the plan.
6356
@plan = fetch_plan_with_associations(p_params[:plan_id])
6457

@@ -118,7 +111,6 @@ def create_or_update
118111
}.to_json
119112
end
120113
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
121-
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
122114

123115
private
124116

0 commit comments

Comments
 (0)