Skip to content

Commit

Permalink
Fix users list sorting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sauloperez committed Jan 30, 2018
1 parent 3708648 commit 2d3bb1c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2d3bb1c

Please sign in to comment.