PERF: remove N+1 query from assign

PERF: remove N+1 query from assign

Also makes looking for allowed groups much more efficient

diff --git a/plugin.rb b/plugin.rb
index 76fce77..8b871a5 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -49,8 +49,13 @@ after_initialize do
   reviewable_api_enabled = defined?(Reviewable)
 
   add_to_class(:user, :can_assign?) do
-    allowed_groups = SiteSetting.assign_allowed_on_groups.split('|')
-    (groups.pluck(:name) & allowed_groups).present?
+    @can_assign ||=
+      begin
+        allowed_groups = SiteSetting.assign_allowed_on_groups.split('|').compact
+        allowed_groups.present? && groups.where('name in (?)', allowed_groups).exists? ?
+          :true : :false
+      end
+    @can_assign == :true
   end
 
   add_to_class(:guardian, :can_assign?) { user && user.can_assign? }

GitHub sha: fe746fef

2 Likes

(I feel I already asked that question but I can’t find it)

How is using symbols :true and :false better than using native booleans true and false?

This is a trick to make ||= work, otherwise we don’t memoize false and keep repeating work

1 Like

Duh

2 Likes