diff --git a/app/models/log.rb b/app/models/log.rb index 195f63ba7..00a178e2b 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -50,7 +50,13 @@ class Log < ApplicationRecord scope :has_old_form_id, -> { where.not(old_form_id: nil) } scope :imported_2023_with_old_form_id, -> { imported.filter_by_year(2023).has_old_form_id } scope :imported_2023, -> { imported.filter_by_year(2023) } - scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } + # TODO: CLDC-4273: use .union in filter_by_organisation rather than raw SQL + scope :filter_by_organisation, lambda { |orgs, _user = nil| + owned = unscoped { where(owning_organisation: orgs).select(:id) } + managed = unscoped { where(managing_organisation: orgs).select(:id) } + + where("#{table_name}.id = ANY(ARRAY(#{owned.to_sql} UNION #{managed.to_sql}))") + } scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) } scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: User.visible(user).search_by(param)) }