Fix and Refactor Querying of template.phases and Associations Within OrgAdmin TemplatesController
#3472
+11
−23
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes # .
Changes proposed in this PR:
Address ambiguity and remove one-to-many columns from
.select()clause ca52b91.select()clause. Since 'sections.title' was listed after 'phases.title',phases.first.title, would evaluate to thesections.titlevalue.sections.titlein.select()is not appropriate..includes(sections: { questions: :question_options })to preload the associations, andphase.sections.eachetc. handles the associations properly within the view.Refactor fetching of
template.phasesb18a454phasesqueries within theviewandeditactions. This refactor makes the query accessible by calling thefetch_template_phasesmethod.Fix and refactor ordering of template associations c7921b4
.order()as it was only applied to the main phases query and did not guarantee the order of associated records (e.g.,question_options), which are loaded in separate queries.default_scope { order(number: :asc) }already exists for thePhase,Section, andQuestionmodels, ensuring the desired ordering by default.QuestionOptionmodel includesscope :by_number, -> { order(:number) }, which is now explicitly applied when fetching thequestion_optionsassociation to enable proper ordering..order()must've been auto-including:idin the.select()clause. Since it is required, we are explicitly adding it now.Refactor/optimise check for
question_optionsdb92f3b.length > 0with.any?to improve readability and performance..any?is more efficient for checking if a collection has elements, especially for ActiveRecord associations, as it doesn't require loading the entire collection into memory.