From be834ba58d00fcfc35dd124c5bc4985373e4b046 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 15 Mar 2022 12:51:01 +0000 Subject: [PATCH] models: reload old_user roles before destroying it Otherwise ActiveRecord believes that old_user still has roles (although they have been migrated to the new user), and attempts to delete them. --- app/models/user.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 4da9b3382..441737c09 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -221,6 +221,7 @@ class User < ApplicationRecord old_user.invites.update_all(user_id: id) old_user.merge_logs.update_all(user_id: id) + # Move or merge old user's roles to the user [ [old_user.instructeur, instructeur], [old_user.expert, expert], @@ -232,6 +233,8 @@ class User < ApplicationRecord targeted_role.merge(old_role) end end + # (Ensure the old user doesn't reference its former roles anymore) + old_user.reload merge_logs.create(from_user_id: old_user.id, from_user_email: old_user.email) old_user.destroy