From 53f6e4b41f77031af08ffc663c48d670ff71a6f4 Mon Sep 17 00:00:00 2001 From: Owajigbanam Ogbuluijah Date: Fri, 6 Feb 2026 14:46:02 +0100 Subject: [PATCH] fix(Deduplicator): Duplicate Immutable Associations --- .../deduplicators/for_immutable_entity.rb | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app/services/one_off/opensrp/deduplicators/for_immutable_entity.rb b/app/services/one_off/opensrp/deduplicators/for_immutable_entity.rb index 943b79e9ce..bc2f425813 100644 --- a/app/services/one_off/opensrp/deduplicators/for_immutable_entity.rb +++ b/app/services/one_off/opensrp/deduplicators/for_immutable_entity.rb @@ -9,14 +9,30 @@ def initialize old_id, new_id, assoc = nil end def merge - old_patient.send(@association).each do |associated| - associated.patient = new_patient - associated.save! + if is_has_many?(@association) + old_patient.send(@association).each do |associated| + new_patient.send(@association) << associated.dup + end + else + old_patient.send(@association).each do |associated| + duplicate = associated.dup + duplicate.patient = new_patient + duplicate.save! + end end # return the old patient object for the chain old_patient end + + private + + def is_has_many? assoc + Patient + .reflect_on_all_associations(:has_many) + .map(&:name) + .include?(assoc) + end end end end