FIX: Destroying a user failed when it had title

FIX: Destroying a user failed when it had title

…because updating the user caused a validation of the primary email and the UserEmail records are already deleted at that time.

diff --git a/app/models/group_user.rb b/app/models/group_user.rb
index 664a333..7b8d865 100644
--- a/app/models/group_user.rb
+++ b/app/models/group_user.rb
@@ -38,7 +38,7 @@ class GroupUser < ActiveRecord::Base
 
   def grant_other_available_title
     if group.title.present? && group.title == user.title
-      user.update!(title: user.next_best_title)
+      user.update_attribute(:title, user.next_best_title)
     end
   end
 
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index 0cb8362..ddc3224 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -502,6 +502,14 @@ describe Group do
       expect(user.title).to eq('Different')
       expect(user.primary_group).to eq(primary_group)
     end
+
+    it "doesn't fail when the user gets destroyed" do
+      group.update(title: 'Awesome')
+      group.add(user)
+      user.reload
+
+      UserDestroyer.new(Discourse.system_user).destroy(user)
+    end
   end
 
   it "has custom fields" do

GitHub sha: 13d5cced

1 Like