From 9d2f23a71d16967696d7ee29eab874b30b8bc8ae Mon Sep 17 00:00:00 2001 From: Matt Farnsworth Date: Mon, 14 Dec 2015 14:08:52 +0200 Subject: [PATCH] Story #105846070 - use Domain.includes, replace Domain.find; return Relation rather than array. white space changes --- app/models/business_registry_cache.rb | 28 +++++++++++++-------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/app/models/business_registry_cache.rb b/app/models/business_registry_cache.rb index 5defd4183..9bc65d599 100644 --- a/app/models/business_registry_cache.rb +++ b/app/models/business_registry_cache.rb @@ -20,21 +20,19 @@ authentication using electronic ID. Association through a business organisation class BusinessRegistryCache < ActiveRecord::Base - def associated_domains - domains = [] - contact_ids = associated_businesses.map do |bic| - Contact.select(:id).where("ident = ? AND ident_type = 'org' AND ident_country_code = 'EE'", bic).pluck(:id) - end - contact_ids = Contact.select(:id).where("ident = ? AND ident_type = 'priv' AND ident_country_code = ?", - ident, ident_country_code).pluck(:id) + contact_ids - contact_ids.flatten!.compact! unless contact_ids.blank? - contact_ids.uniq! unless contact_ids.blank? - unless contact_ids.blank? - DomainContact.select(:domain_id).distinct.where("contact_id in (?)", contact_ids).pluck(:domain_id).try(:each) do |domain_id| - domains << Domain.find(domain_id) - end - end - domains + def associated_domains + domains = [] + contact_ids = associated_businesses.map do |bic| + Contact.select(:id).where("ident = ? AND ident_type = 'org' AND ident_country_code = 'EE'", bic).pluck(:id) + end + contact_ids = Contact.select(:id).where("ident = ? AND ident_type = 'priv' AND ident_country_code = ?", + ident, ident_country_code).pluck(:id) + contact_ids + contact_ids.flatten!.compact! unless contact_ids.blank? + unless contact_ids.blank? + contact_ids.uniq! + domains = DomainContact.select(:domain_id).distinct.where("contact_id in (?)", contact_ids).pluck(:domain_id) + end + Domain.includes(:registrar, :registrant).where('id in (?)', domains) end class << self