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