From c01cee4aa63255d110601fb9465463eb329521dc Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 27 Jan 2017 10:53:42 +0800 Subject: [PATCH] PERF: N+1 query when saving a user. --- app/models/user.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 0efcb78c84a..371c89757e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -933,12 +933,13 @@ class User < ActiveRecord::Base end def automatic_group_membership + user = User.find(self.id) + Group.where(automatic: false) .where("LENGTH(COALESCE(automatic_membership_email_domains, '')) > 0") .each do |group| domains = group.automatic_membership_email_domains.gsub('.', '\.') - user = User.find(self.id) if user.reload.email =~ Regexp.new("@(#{domains})$", true) && !group.users.include?(user) group.add(user)