From dd305b2232b5658c0a80046c0b2bb2d79d3f5bc0 Mon Sep 17 00:00:00 2001 From: jorg-vr Date: Tue, 3 Oct 2023 10:26:15 +0200 Subject: [PATCH] always do some sorting and replace pluck by subquery --- app/controllers/submissions_controller.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index a6fbfcf374..2c1d0254a5 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -194,11 +194,14 @@ def set_submissions # to be applied before this one @submissions = @submissions.most_recent_per_user - return if params[:order_by].blank? - - # reapplies the order_by scope if present in the params - # this is needed because the previous line creates a group by query, which breaks the order_by scope - @submissions = Submission.where(id: @submissions.pluck(:id)) # otherwise the group_by breaks order_by scopes that use joins - @submissions = apply_scopes(@submissions, { order_by: params[:order_by] }) + if params[:order_by].present? + # reapplies the order_by scope if present in the params + # this is needed because the previous line creates a group by query, which breaks the order_by scope + @submissions = Submission.where(id: @submissions) # otherwise the group_by breaks order_by scopes that use joins + @submissions = apply_scopes(@submissions, { order_by: params[:order_by] }) + else + # reapply the default order scope, as most_recent_per_user breaks it + @submissions = @submissions.reorder(id: :desc) + end end end