---
# System prepended metadata

title: Patient <---> ContactPerson Relationship

---

# Patient <---> ContactPerson Relationship

### Business cases
 - patient is a family member of a user, independent (wife/houseband/partner)
 - patient is a family member of a user, dependent (minor, elderly with user being legal custodian or sth)
 - patient is not related anyhow to user (elderly person in nursing home or sth or handicapped person with social custodian or sth)

```ruby=
class Patient < ApplicationRecord
  has_many :contact_link, optional: true # safeguard for a case in which user IS the patient? in that case no contact person needed
  has_many :contact_entity, through: :contact_person_link
  belongs_to :contacted_patient # in case patient is a contact to some other patient
end
```
> To check if **has_one** makes sense or if **has_many** would be better

```ruby=
# join table Polymorphic assoc
class ContactLink < ApplicationRecord
    belongs_to :patient
    belongs_to :contact_entity, polymorphic: true
    enum relationship: {family: 1, non_family: 2} # to think about
end

class ContactPerson < ApplicationRecord
    belongs_to :contacted_patient # entity to which this contact_person is a contact (being patient) 
end
```



```csvpreview {header="true"}
patient_id, contact_entity_type, contact_entity_id, relationship
1, User, 3, 1
2, Patient, 4, 1
3, ContactPerson, 5, 2
```

Patiens and ConctacPerson could at some point be an STI, to prevent clutter and having too many DB tables

```plantuml
@startuml


class ContactLink {
    + patient
    + contact_entity
}

class ContactEntity {
    + contact_entity_type
    + contact_entity_id
}



package ContactEntitiesTypes #0F0 {
class Patient {
    + contact_link
    + contacted_patients[]
}

class ContactPerson {
    + contacted_patients[]
}

class User {
    + contacted_patients[]
}
    ContactEntity o-- ContactPerson
    ContactEntity o-- Patient
    ContactEntity o-- User
}


ContactEntity }|--|| ContactLink
Patient ||--|{ ContactLink

note top of ContactEntity #0F0: Polymorphic

@enduml
```





