From 5ef9d4d5371d8bad557168de643a13a483be1ebc Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 9 Sep 2020 14:57:07 +0800 Subject: [PATCH] PERF: Use 1 query to load group and tag notification default. --- app/serializers/basic_group_serializer.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/serializers/basic_group_serializer.rb b/app/serializers/basic_group_serializer.rb index 3fee36fcba5..8004e7ccdc3 100644 --- a/app/serializers/basic_group_serializer.rb +++ b/app/serializers/basic_group_serializer.rb @@ -134,11 +134,23 @@ class BasicGroupSerializer < ApplicationSerializer [:watching, :regular, :tracking, :watching_first_post, :muted].each do |level| define_method("#{level}_category_ids") do - GroupCategoryNotificationDefault.lookup(object, level).pluck(:category_id) + @group_category_notification_defaults ||= begin + GroupCategoryNotificationDefault.where(group: object).pluck(:category_id) + end + + @group_category_notification_defaults.filter do |default| + default.notification_level == NotificationLevels.all[level] + end end define_method("#{level}_tags") do - GroupTagNotificationDefault.lookup(object, level).joins(:tag).pluck('tags.name') + @group_tag_notification_defaults ||= begin + GroupTagNotificationDefault.where(group: object).joins(:tag).pluck('tags.name') + end + + @group_tag_notification_defaults.filter do |default| + default.notification_level == NotificationLevels.all[level] + end end end