From 8fe1e2bf712d81a1da7a0e44c86028574b067d1e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 26 Oct 2021 15:50:38 +0200 Subject: [PATCH] wrap merge in transaction --- app/models/user.rb | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f12c9b107..24656f846 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -196,24 +196,26 @@ class User < ApplicationRecord end def merge(old_user) - old_user.dossiers.update_all(user_id: id) - old_user.invites.update_all(user_id: id) - old_user.merge_logs.update_all(user_id: id) + transaction do + old_user.dossiers.update_all(user_id: id) + old_user.invites.update_all(user_id: id) + old_user.merge_logs.update_all(user_id: id) - [ - [old_user.instructeur, instructeur], - [old_user.expert, expert], - [old_user.administrateur, administrateur] - ].each do |old_role, targeted_role| - if targeted_role.nil? - old_role&.update(user: self) - else - targeted_role.merge(old_role) + [ + [old_user.instructeur, instructeur], + [old_user.expert, expert], + [old_user.administrateur, administrateur] + ].each do |old_role, targeted_role| + if targeted_role.nil? + old_role&.update(user: self) + else + targeted_role.merge(old_role) + end end - end - merge_logs.create(from_user_id: old_user.id, from_user_email: old_user.email) - old_user.destroy + merge_logs.create(from_user_id: old_user.id, from_user_email: old_user.email) + old_user.destroy + end end private