diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index fb7f8f2ad..0ac0e4bf5 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -54,7 +54,7 @@ class LettingsLog < Log scope :unresolved, -> { where(unresolved: true) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } - scope :filter_by_owning_organisation, ->(org, _user = nil) { where(owning_organisation: org) } + 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 :duplicate_logs, lambda { |log| diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 47a3ef9f9..25d555e77 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -40,8 +40,7 @@ class SalesLog < Log .or(filter_by_postcode(param)) .or(filter_by_id(param)) } - scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } - scope :filter_by_owning_organisation, ->(org, _user = nil) { where(owning_organisation: org) } + scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :duplicate_logs, lambda { |log| visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where.not(id: log.id) diff --git a/app/models/user.rb b/app/models/user.rb index 3fe10b604..05a071f8a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,9 +7,6 @@ class User < ApplicationRecord # Marked as optional because we validate organisation_id below instead so that # the error message is linked to the right field on the form belongs_to :organisation, optional: true - has_many :owned_lettings_logs, through: :organisation - has_many :managed_lettings_logs, through: :organisation - has_many :owned_sales_logs, through: :organisation has_many :legacy_users has_many :bulk_uploads @@ -68,10 +65,22 @@ class User < ApplicationRecord if support? SalesLog.all else - SalesLog.filter_by_organisation(organisation.absorbed_organisations << organisation) + SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations << organisation) end end + def owned_lettings_logs + LettingsLog.filter_by_owning_organisation(organisation.absorbed_organisations << organisation) + end + + def managed_lettings_logs + LettingsLog.filter_by_managing_organisation(organisation.absorbed_organisations << organisation) + end + + def owned_sales_logs + SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations << organisation) + end + def is_key_contact? is_key_contact end