diff --git a/app/models/organisation.rb b/app/models/organisation.rb index a4201d089..b073bb373 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -1,5 +1,5 @@ class Organisation < ApplicationRecord has_many :users - has_many :case_logs, as: :owning_organisation - has_many :case_logs, as: :managing_organisation + has_many :owned_case_logs, class_name: "CaseLog", foreign_key: "owning_organisation_id" + has_many :managed_case_logs, class_name: "CaseLog", foreign_key: "managing_organisation_id" end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 2c2363c47..530ff1acc 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -12,5 +12,31 @@ RSpec.describe Organisation, type: :model do it "has users" do expect(organisation.users.first).to eq(user) end + + context "case logs" do + let(:other_organisation) { FactoryBot.create(:organisation) } + let!(:owned_case_log) do + FactoryBot.create( + :case_log, + owning_organisation: organisation, + managing_organisation: other_organisation + ) + end + let!(:managed_case_log) do + FactoryBot.create( + :case_log, + owning_organisation: other_organisation, + managing_organisation: organisation + ) + end + + it "has owned case logs" do + expect(organisation.owned_case_logs.first).to eq(owned_case_log) + end + + it "has managed case logs" do + expect(organisation.managed_case_logs.first).to eq(managed_case_log) + end + end end end