From a88e936693db2108761ecaf500720a6dff4810fd Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Mon, 10 Oct 2022 15:48:38 +0200 Subject: [PATCH] Fix filtering by user role for teams table [SCI-7230] --- app/datatables/teams_datatable.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/datatables/teams_datatable.rb b/app/datatables/teams_datatable.rb index 6614b231a..a760d3946 100644 --- a/app/datatables/teams_datatable.rb +++ b/app/datatables/teams_datatable.rb @@ -64,9 +64,15 @@ class TeamsDatatable < CustomDatatable records.reverse end elsif sort_column(order_params) == 'user_roles.name' - records_with_role = records.where(user_assignments: { user: @user }) + records_with_role = records.joins(user_assignments: :user) + .where(user_assignments: { user: @user }) .sort_by(&proc { |team| current_user_team_role(team)&.name }) - records_with_no_role = records.where.not(user_assignments: { user: @user }) + records_with_no_role = + records.joins("LEFT OUTER JOIN user_assignments AS current_user_assignments "\ + "ON current_user_assignments.assignable_type = 'Team' "\ + "AND current_user_assignments.assignable_id = teams.id "\ + "AND current_user_assignments.user_id = #{@user.id}") + .where(current_user_assignments: { id: nil }) records = records_with_no_role + records_with_role if order_params['dir'] == 'asc' records