PERF: N+1 query when saving a user.

This commit is contained in:
Guo Xiang Tan 2017-01-27 10:53:42 +08:00
parent 156a00af47
commit c01cee4aa6
1 changed files with 2 additions and 1 deletions

View File

@ -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)