Different users with different information and privileges

I am writing a website to manage appointments between doctors and patients in a medical clinic and I have a question regarding users:
There should be 4 types of users: Admin, Doctor, Receptionist and patient.
As for now I have a user model and a role model to determine which user is which. The problem with this setup is that I want to be able to assign a patient to a doctor so that suggests to me that I should have a patient and a doctor model. Is that right? if so should I scrap my user model and make one for each type of user? can I extend the user model and keep on using it?
Also I should mention, I am a total newbie :slight_smile:

A single ‘user’ type is fine, the roles distinguish who is who. To assign a patient to a doctor you’d just make another intermediate table that holds a doctor id and a patient id (called a many-to-many relation in SQL parlance), you can add more info on that intermediary table too like when that was first setup or whatever else extra too. :slight_smile:

Also, remember the creed of SQL Normalization (tons of things on google about it) to keep your database easier to extend and functioning quickly. :slight_smile:

3 Likes

Thank you for your quick reply :smile:
I have some doubt on how to technically do it: I create a migration and create a new table patient_doctor_relationship then i need to reference the the ids of the users right? can I make a query in the migration? like :patient_id, references user_id where role_id == x and then the same for doctor?
thank you
carlo

In patient_doctor_relationship schema you can try that

belongs_to :patient, YourApp.User, source: :patient_id
belongs_to :doctor, YourApp.User, source: :doctor_id
2 Likes

Thanks for the help, the problem is that I don’t have a patient_id as source I don’t think if I understand the suggestion correctly. Maybe if i post the schema it’ll better explain the issue

schema "users" do
    field :title, :string
    field :f_name, :string
    field :l_name, :string
    field :DOB, :date
    field :email, :string
    field :username, :string
    field :password, :string, virtual: true
    field :password_confirmation, :string, virtual: true
    field :password_hash, :string
    
    belongs_to :role, Assistant.Role

    timestamps()
end

the role_id simply correspond to 4 differnt ids for the different roles in the role table

thank you

not user schema. You should use in patient_doctor_relationship schema. Also maybe there can be better approaches. I am not experienced programmer. Here examples codes

schema "patient_doctor_relationship" do
    field :patient_id, :integer
    field :doctor_id, :integer

belongs_to :patient, YourApp.User, source: :patient_id
belongs_to :doctor, YourApp.User, source: :doctor_id
end

 schema "users" do
    ....
    ....
 has_many :doctor_relationships, YourApp.PatientDoctorRelationship, foreign_key: :patient_id
 has_many :patient_relationships, YourApp.PatientDoctorRelationship, foreign_key: :doctor_id
end

I see, so in this way, say I wanna assign a patient to a doctor and vice versa I just create a new patient_relationship, with the relevant ids, add it to the patient_doctor_relationship table and that’it right?

Actually it depends on your app. Do you want patient to choose time slot for a doctor or that patient will be assigned to doctor for a long time ?

Kinda both, meaning that a patient will require an appointment, he/she will be assigned a slot and a doctor, but once a doctor visits that patient he/she should always be listed as his/her patients with the relevant data from tehir appointment

So from understanding you dont need patient_doctor_relationship. You just need to add reservations to that. You can fetch patients from reservations

schema "reservations" do

    other fields
     ....
    field :patient_id, :integer
    field :doctor_id, :integer

belongs_to :patient, YourApp.User, source: :patient_id
belongs_to :doctor, YourApp.User, source: :doctor_id
end
1 Like

I see,
So if I just create the reservation table i add those two id’s and then the date time and details about the appointment and it’s all good. Then when I show the patients of a doctor I just query the db and fetch the id of patient corresponding to theirs… cool thanks

I am building a booking app smilar to yours. What i do is i have weekly time slots. And another bookings table. In show screen depends of a reservation exist for a slot and date. Slots are looking closed. If no booking then user can create a booking. Maybe some forum members can have better approaches.

1 Like

oh cool! can I ask you another question then? How did you go about building a sort of appointment calendar for the user? I would like to build 2 calendars one for shifts for the admin to manage the doctors and one for appointments

Sorry I think I might be a bit daft but I have another question :slight_smile:
in the belongs_to :patient i do not have the field patient in my user schema, is it possible to do something like belongs_to :user, where user.role_id = x? I mean is it possible to do a query inside the belongs_to statement or should I do an association somewhere else?
thanks :slight_smile:

Yes it is possible. Check that link for more information. Patient and doctor both are user. You dont need to specify where user.role_id

1 Like