Skip to content

Commit ef06465

Browse files
committed
Update answers_controller
1 parent 2535269 commit ef06465

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

app/controllers/answers_controller.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,32 @@ def create_or_update
9898
@section = @plan.sections.find_by(id: @question.section_id)
9999
template = @section.phase.template
100100

101-
remove_list_after = remove_list(@plan)
102-
101+
# Get list of questions to be removed from the plan based on any conditional questions.
102+
questions_remove_list_before_destroying_answers = remove_list(@plan)
103103
all_question_ids = @plan.questions.pluck(:id)
104-
# rubocop pointed out that these variable is not used
105-
# all_answers = @plan.answers
104+
105+
# Destroy all answers for removed questions
106+
questions_remove_list_before_destroying_answers.each do |id|
107+
Answer.where(question_id: id, plan: @plan).each do |a|
108+
Answer.destroy(a.id)
109+
end
110+
end
111+
# Now update @plan after removing answers of questions removed from the plan.
112+
@plan = Plan.includes(
113+
sections: {
114+
questions: %i[
115+
answers
116+
question_format
117+
]
118+
}
119+
).find(p_params[:plan_id])
120+
121+
# Now get list of question ids to remove based on remaining answers.
122+
remove_list_question_ids = remove_list(@plan)
123+
106124
qn_data = {
107-
to_show: all_question_ids - remove_list_after,
108-
to_hide: remove_list_after
125+
to_show: all_question_ids - remove_list_question_ids,
126+
to_hide: remove_list_question_ids
109127
}
110128

111129
section_data = []

0 commit comments

Comments
 (0)