From f0558ae0bfb66b4a680a851882a500bc1fc203b7 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Mon, 25 Jan 2016 18:15:18 +0200 Subject: [PATCH] Story#105846070 - more rails way how to load relations --- app/models/business_registry_cache.rb | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/app/models/business_registry_cache.rb b/app/models/business_registry_cache.rb index e287358b6..5ca1e002c 100644 --- a/app/models/business_registry_cache.rb +++ b/app/models/business_registry_cache.rb @@ -20,37 +20,28 @@ authentication using electronic ID. Association through a business organisation class BusinessRegistryCache < ActiveRecord::Base + # 1. load domains by business + # 2. load domains by person def associated_domains domains = [] - contact_ids = associated_businesses.map do |bic| - Contact.where("ident = ? AND ident_type = 'org' AND ident_country_code = 'EE'", bic).pluck(:id) - end.flatten - contact_ids += Contact.where("ident = ? AND ident_type = 'priv' AND ident_country_code = ?", - ident, ident_country_code).pluck(:id) + + contact_ids = Contact.where(ident_type: 'org', ident: associated_businesses, ident_country_code: 'EE').pluck(:id) + contact_ids += Contact.where(ident_type: 'priv', ident: ident, ident_country_code: ident_country_code).pluck(:id) unless contact_ids.blank? - domains = DomainContact.select(:domain_id).distinct.where(contact_id: contact_ids).pluck(:domain_id) + domains = DomainContact.distinct.where(contact_id: contact_ids).pluck(:domain_id) end Domain.includes(:registrar, :registrant).where(id: domains) end class << self - def fetch_associated_domains(ident_code, ident_cc) - cached = fetch_by_ident_and_cc(ident_code, ident_cc) - if cached.blank? - Domain.includes(:registrar, :registrant).where(contacts: { - ident_type: 'priv', - ident: ident_code, - ident_country_code: ident_cc}) - else - cached.associated_domains - end + fetch_by_ident_and_cc(ident_code, ident_cc).associated_domains end def fetch_by_ident_and_cc(ident_code, ident_cc) - cache = BusinessRegistryCache.first_or_initialize(ident: ident_code, ident_country_code: ident_cc) + cache = BusinessRegistryCache.where(ident: ident_code, ident_country_code: ident_cc).first_or_initialize msg_start = "[Ariregister] #{ident_cc}-#{ident_code}:" # fetch new data if cache is expired