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.
This commit is contained in:
Pierre de La Morinerie 2022-03-15 12:51:01 +00:00
parent 2cac7d96be
commit be834ba58d

View file

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