Let's take the example from the association documentation:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
My question is, supposing the Appointment
model has an appointment_date
field how can I set it most elegantly when working with new physicians and patients?
For now my solution for creation is, that instead of using:
physician = Physician.new(...)
patient = Patient.new(...)
physician.patients < patient
physician.save
I use:
physician = Physician.new(...)
patient = Patient.new(...)
appointment = Appointment.new
appointment.physician = physician
appointment.patient = patient
appointment.appointment_date = Time.now
appointment.save
And for querying I found the following solution.
Is there a shorter / more succinct / "Rails" way?