File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 = [ ]
You can’t perform that action at this time.
0 commit comments