diff --git a/app/models/location.rb b/app/models/location.rb index 2b652c659..8efa4ee28 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -18,8 +18,8 @@ class Location < ApplicationRecord auto_strip_attributes :name, squish: true - scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } - scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } + scope :search_by_postcode, ->(postcode) { where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } + scope :search_by_name, ->(name) { where("locations.name ILIKE ?", "%#{name}%") } scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } scope :started, -> { where("locations.startdate <= ?", Time.zone.today).or(where(startdate: nil)) } scope :started_in_2_weeks, -> { where("locations.startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) } diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 44118bcc6..2f991ab31 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -1019,6 +1019,13 @@ RSpec.describe Location, type: :model do expect(described_class.filter_by_status(%w[deactivating_soon activating_soon])).to include(deactivating_soon_location) end end + + context "when filtering by a status and searching" do + it "returns only active locations" do + expect(described_class.filter_by_status(%w[active]).search_by(active_location.name).count).to eq(1) + expect(described_class.filter_by_status(%w[active]).search_by(active_location.name).first).to eq(active_location) + end + end end describe "available_from" do