From 4af4c948e91e2fe8b2a29abc0996cf27b3edca6b Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Wed, 10 Sep 2025 14:51:30 +0100 Subject: [PATCH] CLDC-4055: Use name property for filtering organisations (#3097) --- app/models/organisation.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 503cee7ba..94390c3f9 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -38,7 +38,13 @@ class Organisation < ApplicationRecord Organisation.where(id: ids) end - scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } + scope :search_by_name, lambda { |name| + # we can't use the activerecord queries to query the DB as the `name` col doesn't update when the org changes name. + # this is due to the info being stored in the organisation_name_changes table. + # so we load all and filter in memory. the number of organisations is small so this should be fine. + matched_ids = all.filter { |org| org.name.downcase.include?(name.downcase) }.map(&:id) + where(id: matched_ids) + } scope :search_by, ->(param) { search_by_name(param) } scope :filter_by_active, -> { where(active: true) } scope :filter_by_inactive, -> { where(active: false) }