How to store data through a table linked to one of the join tables
Using the example from the Doctor and Patient rail guide, then attach them to the Assignment table.
If the Doctor has multiple questions, what is the best way to store unique responses from patients?
Possible solution: Patient has_many Surveys and Survey has_many Answers. Create this when a new Assignment is created. Problem: I find that views and controllers are complex enough for something as simple as, to reiterate my question, store data associated with a join table.
source to share
At the heart of all this, it seems to me that you have the answer - to the question, during the meeting, from the patient to the doctor.
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :appointment
belongs_to :patient
belongs_to :physician
end
So we also need models of questions, prescriptions, patients, doctors who "have many" answers.
Questions may belong to individual doctors (although I think many doctors ask the same questions).
When you create an appointment, you know the doctor and the patient. Write an Assignment, then select Physician Questions and create answer records for each of the Physician Questions (you now know the IDs of all four related entities). Do this in your assignment model, not your controller. Have a look at ActiveRecord :: Callbacks, after_create can be your friend in this case.
In this case, the query becomes very simple and record ids (foreign keys) are relatively cheap to store.
An interesting scenario.
source to share
Assuming doctors do not separate questions, the following models describe what you are trying to accomplish.
Models (denormalized):
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
has_many :questions
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :answers
has_many :physicians, through: :appointments
end
class Questions < ActiveRecord::Base
belongs_to :physician
has_one :answer
end
class Answers < ActiveRecord::Base
belongs_to :question
belongs_to :patient
end
Edit Regarding Requests (this is not as hard as you expected)
Receiving all patients for the doctor:
Physician.find(1).patients
Getting all questions for the doctor:
Physician.find(1).questions
Getting all responses for the patient:
Patient.find(1).answers
source to share