Example from Guide with additional property in association model
class CreateAppointments < ActiveRecord::Migration
def change
create_table :physicians do |t|
t.string :name
t.timestamps null: false
end
create_table :patients do |t|
t.string :name
t.timestamps null: false
end
create_table :appointments do |t|
t.belongs_to :physician, index: true
t.belongs_to :patient, index: true
t.datetime :appointment_date
t.timestamps null: false
end
end
end
Model Appointment has a validation:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
validates :appointment_date, presence: true
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
When I add patient to physicians some_physician.patient << some_patient
I have to define appointment_date
. How to do it correctly?