I am trying to figure out how to set up my Seeds.rb data manually. With this data, I'm trying to connect the following relationships: A doctor has many patients , a patient has one doctor, a patient has many charts and a doctor has access to the charts through patients.
I'm not understanding how to connect the three different models when i'm creating the data manually.
- For the Doctor.rb model:
class Doctor < ActiveRecord::Base
has_many :patients
has_many :charts , through: :patients
end
- Migration for Doctor
class CreateDoctors < ActiveRecord::Migration[6.1]
def change
create_table :doctors do |t|
t.string :name , :speciality
t.integer :license_number
end
end
end
- Patient.rb model:
class Patient < ActiveRecord::Base
belongs_to :doctor
has_many :charts
end
- Migration for Patient
class CreatePatients < ActiveRecord::Migration[6.1]
def change
create_table :patients do |t|
t.string :name , :gender, :description, :medication
t.integer :age
t.float :account_balance
t.integer :doctor_id , chart_id
end
end
end
- Chart.rb model
class Chart < ActiveRecord::Base
belongs_to :patient
belongs_to :doctor
end
- Migration for Chart
class CreateCharts < ActiveRecord::Migration[6.1]
def change
create_table :charts do |t|
t.string :chart
t.integer :patient_id , :doctor_id
end
end
end
example of seed data: (im not sure how to connect the three)
Doctor.create(name: "Aaron Yan", speciality: "Marriage & Family " , license_number: 85477)
Patient.create(name: "Danielle Marsh", age: 28, gender: "F", account_balance: 44.23 , description: "Panic Disorder, Insomnia", medication:"Tofranil 25 mg")
Chart.create(description: "this patient exhibits the following symptoms, treatment plan is etc")
Hi and welcome to Stack overflow! To create related data, you just need to save the result to variables and add them using the relationships. eg: