Reputation: 350
The documentation is a little poor in this aspect. If I have this tables
Physicians Table
-----------
id integer
name string
Appointments table
----------------
id integer
physician_id integer
patient_id integer
appointment_date datetime
Patients table
---------------
id integer
name string
and this models:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :pshysician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :pshysicians, :through => :appointments
end
how do I do to insert an appointment on the database? It's not clear at all in the Rails webpage
Upvotes: 2
Views: 82
Reputation: 4322
This is a quite broad question actually. To insert a record in the database you can do one of three things.
Appointment.create(patient_id: 1, physician_id: 1, apt_date: DateTime.now)
If you have an instance of the Patient
model lets call it patient
and you want to assign in to physician with id=2
then
patient.appointments.create(physician_id: 2, apt_date: DateTime.now)
If you have an instance of the Physician
model called physician
then
physician.appointments.create(patient_id: 10, apt_date: DateTime.now)
Upvotes: 1