From af037edcdcbc66b33d1feb748ed04e3f462f7af1 Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Fri, 1 Nov 2024 12:06:56 +0000 Subject: [PATCH] Don't skip check if eligibility is missing attrs If the two eligibilities don't have all the same attributes, rather than skipping the comparison only compare the attributes they have in common. Also uses select rather than pluck so we don't need to run a query for each iteration of the loop --- app/models/claim/matching_attribute_finder.rb | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/app/models/claim/matching_attribute_finder.rb b/app/models/claim/matching_attribute_finder.rb index 410623c701..04ec51ee0c 100644 --- a/app/models/claim/matching_attribute_finder.rb +++ b/app/models/claim/matching_attribute_finder.rb @@ -37,15 +37,24 @@ def matching_claims end # Eligibility attributes - eligibility_ids = eligibility_attributes_groups_to_match.map { |attributes| + eligibility_ids = eligibility_attributes_groups_to_match.flat_map do |attributes| vals = values_for_attributes(@source_claim.eligibility, attributes) next if vals.blank? - concatenated_columns = "CONCAT(#{attributes.join(",")})" # current policies will only have a single value in attributes - policies_to_find_matches.map { |policy| - policy::Eligibility.where("LOWER(#{concatenated_columns}) = LOWER(?)", vals.join) if all_attributes_exist_on_the_eligibility(attributes, policy::Eligibility) - } - }.flatten.compact.map(&:id) + policies_to_find_matches.map do |policy| + # Not all eligibility models have the same columns + attributes_to_check = attributes & policy::Eligibility.column_names + + if attributes_to_check.any? + concatenated_columns = "CONCAT(#{attributes_to_check.join(",")})" + + policy::Eligibility.where( + "LOWER(#{concatenated_columns}) = LOWER(?)", + vals + ).select(:id) + end + end + end.compact_blank eligibility_match_query = Claim.where(eligibility_id: eligibility_ids) match_queries = match_queries.or(eligibility_match_query) @@ -91,9 +100,5 @@ def values_for_attributes(object, attributes) object.read_attribute(attribute) }.reject(&:blank?) end - - def all_attributes_exist_on_the_eligibility(attributes, eligibility) - (attributes - eligibility.column_names).empty? - end end end