From 509bcd0bde7ef941a9fec0c1d53e3137202169b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Garc=C3=ADa=20Isa=C3=ADa?= Date: Mon, 18 Mar 2024 11:07:41 -0300 Subject: [PATCH] Optimize curated addresses (#945) If the address book grows large, the method was really slow when mapping contact addresses to contacts. We now restrict it to only work with the contacts of the addresses it's working on. See #944 --- app/controllers/projects_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index c510660be..e138657d4 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -121,7 +121,8 @@ def curated_addresses(addresses) # build a hash from contact_id to all his addresses # eg. { 1 => ['123','456'], 2 => ['789'] } all_contacts = Hash.new { |hash,key| hash[key] = [] } - all_contacts = @project.contact_addresses.order(:id).inject(all_contacts) do |contacts, contact_address| + addresses_contacts = @project.contact_addresses.where(address: addresses).pluck(:contact_id) + all_contacts = @project.contact_addresses.where(contact_id: addresses_contacts).order(:id).inject(all_contacts) do |contacts, contact_address| contacts[contact_address.contact_id] << contact_address.address contacts end