From 2d3bb1c0967ed5e5a36241c0c3bb9042b232dd57 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 30 Jan 2018 15:03:11 +0100 Subject: [PATCH] Fix users list sorting ActiveRecord doesn't notice that we need to join with `members` in two parts of the ActiveRecord::Relation and it joins it two times adding an alias called `members_users`, instead of joining that table just once. As the constraint on the `organization_id` was referencing the `members_users` table this wasn't picked up by Postgres while joining the tables. Specifically, when joining `users` with `members`, thus returning all members of a user, regardless of their organization. --- app/controllers/users_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dc4b85fb4..b56fba5c2 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,13 +2,14 @@ class UsersController < ApplicationController before_filter :authenticate_user! def index - @search = current_organization.users.ransack(params[:q]) + @search = User.ransack(params[:q]) @search.sorts = 'members_member_uid asc' if @search.sorts.empty? @users = @search .result(distinct: false) .joins(members: :account) .eager_load(members: :account) + .where(members: { organization: current_organization.id }) .page(params[:page]) .per(25)