From 917d186303748f4a1cb7b5689df64e46b0130da9 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 26 Jul 2017 17:18:56 +0900 Subject: [PATCH] REFACTOR: Reduce number of queries sent to the DB. --- app/models/group.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/group.rb b/app/models/group.rb index 18ff63248b9..5ca913f876e 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -457,12 +457,18 @@ class Group < ActiveRecord::Base Group.exec_sql(sql, group_id: self.id, user_ids: user_ids) + new_attributes = {} + if self.primary_group? - User.where(id: user_ids).update_all(primary_group_id: self.id) + new_attributes[:primary_group_id] = self.id end if self.title.present? - User.where(id: user_ids).update_all(title: self.title) + new_attributes[:title] = self.title + end + + if new_attributes.present? + User.where(id: user_ids).update_all(new_attributes) end if self.grant_trust_level.present?