Fix and Refactor Querying of template.phases
and Associations Within OrgAdmin TemplatesController
#991
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 #955
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.title
value.sections.title
in.select()
is not appropriate..includes(sections: { questions: :question_options })
to preload the associations, andphase.sections.each
etc. handles the associations properly within the view.Refactor fetching of
template.phases
b18a454phases
queries within theview
andedit
actions. This refactor makes the query accessible by calling thefetch_template_phases
method.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
, andQuestion
models, ensuring the desired ordering by default.QuestionOption
model includesscope :by_number, -> { order(:number) }
, which is now explicitly applied when fetching thequestion_options
association to enable proper ordering..order()
must've been auto-including:id
in the.select()
clause. Since it is required, we are explicitly adding it now.Refactor/optimise check for
question_options
db92f3b.length > 0
with.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.