diff --git a/app/models/group_user.rb b/app/models/group_user.rb index 57028e8f8fd..7d4a1c2610e 100644 --- a/app/models/group_user.rb +++ b/app/models/group_user.rb @@ -24,14 +24,7 @@ class GroupUser < ActiveRecord::Base end def set_primary_group - if group.primary_group - DB.exec(" - UPDATE users - SET primary_group_id = :id - WHERE id = :user_id", - id: group.id, user_id: user_id - ) - end + user.update!(primary_group: group) if group.primary_group end def remove_primary_group diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index a0bdc24ac80..0a89acf5388 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -710,11 +710,16 @@ describe Group do end it "always sets user's primary group" do - group.update(primary_group: true) + group.update(primary_group: true, title: 'AAAA') expect { group.add(user) }.to change { user.reload.primary_group }.from(nil).to(group) - new_group = Fabricate(:group, primary_group: true) - expect { new_group.add(user) }.to change { user.reload.primary_group }.from(group).to(new_group) + new_group = Fabricate(:group, primary_group: true, title: 'BBBB') + + expect { + new_group.add(user) + user.reload + }.to change { user.primary_group }.from(group).to(new_group) + .and change { user.title }.from('AAAA').to('BBBB') end context 'when adding a user into a public group' do