diff --git a/app/models/ems_refresh/save_inventory_infra.rb b/app/models/ems_refresh/save_inventory_infra.rb index 30c15a6fd8d..2319b1f64f6 100644 --- a/app/models/ems_refresh/save_inventory_infra.rb +++ b/app/models/ems_refresh/save_inventory_infra.rb @@ -64,7 +64,7 @@ def save_ems_infra_inventory(ems, hashes, target = nil, disconnect = true) :orchestration_templates, :orchestration_stacks ] - + old_switch_ids = ems.switches.pluck(:id) # Save and link other subsections save_child_inventory(ems, hashes, child_keys, target, disconnect) @@ -78,7 +78,7 @@ def save_ems_infra_inventory(ems, hashes, target = nil, disconnect = true) new_relats = hashes_relats(hashes) link_ems_inventory(ems, target, prev_relats, new_relats, disconnect) - remove_obsolete_switches + remove_obsolete_switches(ems, old_switch_ids) ems end @@ -331,10 +331,9 @@ def save_switches_inventory(host, hashes) host.switches.delete(deletes) end - def remove_obsolete_switches - # delete from switches as s where s.shared is NULL and s.id not in (select switch_id from host_switches) - # delete from switches as s where s.shared = 't' and s.id not in (select switch_id from host_switches) - Switch.where.not(:id => HostSwitch.all.collect(&:switch).uniq).destroy_all + def remove_obsolete_switches(ems, old_switch_ids) + obsolete_switch_ids = old_switch_ids - ems.switches.reload.pluck(:id) + Switch.where(:id => obsolete_switch_ids).destroy_all end def save_lans_inventory(switch, hashes)