Refactoring: Removed N+1 performance issue by implementing correct join. Thanks to @rolfschmidt!

This commit is contained in:
Thorsten Eckel 2017-10-18 11:16:49 +02:00
parent ef17e67b60
commit 34ec90bd73

View file

@ -300,11 +300,15 @@ module HasGroups
access = ensure_group_access_list_parameter(access) access = ensure_group_access_list_parameter(access)
# check direct access # check direct access
ids = group_through.klass.includes(name.downcase).where(group_id: group_id, access: access, table_name => { active: true }).pluck(group_through.foreign_key) instances = joins(group_through.name)
ids ||= [] .where( group_through.table_name => { group_id: group_id, access: access }, active: true )
# get instances and check for required permission if respond_to?(:permissions?)
instances = where(id: ids).select(&:groups_access_permission?) permissions = Permission.with_parents('ticket.agent')
instances = instances
.joins(roles: :permissions)
.where(roles: { active: true }, permissions: { name: permissions, active: true })
end
# check indirect access through roles if possible # check indirect access through roles if possible
return instances if !respond_to?(:role_access) return instances if !respond_to?(:role_access)