Skip to content

Commit

Permalink
R2-3197 Performance: N+1 for User Groups API
Browse files Browse the repository at this point in the history
  • Loading branch information
aespinoza-quoin committed Feb 11, 2025
1 parent e941c5f commit 71d999f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/user_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def permitted_api_params
end

def list(user, opts = {})
user_groups = opts[:managed] ? user.permitted_user_groups : UserGroup.all
user_groups = opts[:managed] ? user.permitted_user_groups : UserGroup.includes(:agencies).all
user_groups = user_groups.where(disabled: opts[:disabled]) if opts[:disabled].present?
if opts[:agency_unique_ids].present?
user_groups = user_groups.distinct.joins(:agencies).where(agencies: { unique_id: opts[:agency_unique_ids] })
Expand Down
7 changes: 7 additions & 0 deletions spec/models/user_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@
expect(user_groups.size).to eq(3)
expect(user_groups.pluck(:unique_id)).to match_array(%w[group_1 group_2 group_3])
end

it 'when agency filter is not set' do
user_groups = UserGroup.list(nil)

expect(user_groups.size).to eq(3)
expect(user_groups.map(&:unique_id)).to match_array(%w[group_1 group_2 group_3])
end
end
end

0 comments on commit 71d999f

Please sign in to comment.