PERF: Fix N+1 query.
This commit is contained in:
parent
0b28075c00
commit
84914c5e1f
|
@ -1,7 +1,9 @@
|
||||||
class Admin::GroupsController < Admin::AdminController
|
class Admin::GroupsController < Admin::AdminController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
groups = Group.order(:name).where("id <> ?", Group::AUTO_GROUPS[:everyone])
|
groups = Group.order(:name)
|
||||||
|
.where("id <> ?", Group::AUTO_GROUPS[:everyone])
|
||||||
|
.includes(:group_users)
|
||||||
|
|
||||||
if search = params[:search].to_s
|
if search = params[:search].to_s
|
||||||
groups = groups.where("name ILIKE ?", "%#{search}%")
|
groups = groups.where("name ILIKE ?", "%#{search}%")
|
||||||
|
|
|
@ -24,8 +24,7 @@ class BasicGroupSerializer < ApplicationSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def notification_level
|
def notification_level
|
||||||
# TODO: fix this N+1
|
fetch_group_user&.notification_level
|
||||||
GroupUser.where(group_id: object.id, user_id: scope.user.id).first.try(:notification_level)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_notification_level?
|
def include_notification_level?
|
||||||
|
@ -37,11 +36,16 @@ class BasicGroupSerializer < ApplicationSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_member
|
def is_member
|
||||||
scope.is_admin? || GroupUser.where(group_id: object.id, user_id: scope.user.id).present?
|
scope.is_admin? || fetch_group_user.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_is_member?
|
def include_is_member?
|
||||||
scope.authenticated?
|
scope.authenticated?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fetch_group_user
|
||||||
|
@group_user ||= (object.group_users & scope.user.group_users).first
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue