Skip to content

Commit

Permalink
always do some sorting and replace pluck by subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
jorg-vr committed Oct 3, 2023
1 parent 396848f commit dd305b2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dd305b2

Please sign in to comment.