Skip to content

Commit

Permalink
Optimize curated addresses (#945)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matiasgarciaisaia authored Mar 18, 2024
1 parent d55a48d commit 509bcd0
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 509bcd0

Please sign in to comment.