ruby on rails - Has_many, Through: Saving Joiner Table Fail -
so have 3 classes:
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
i trying create new patient record through existing physician record.
doctor = physician.first sick_person = doctor.patients.new sick_person.save
what happening patient record gets saved joiner record, appointments, doesn't created. have been reading few forums , suggested use inverse_of (https://github.com/rails/rails/issues/6161#issuecomment-6330795, seems not right when using :through.
any body have ideas of how joiner table recorded? know create patient record first , create appointments record afterwards patient id, i'm wondering if there more of eloquent solution.
try:
doctor = physician.first sick_person = patient.new doctor.patients << sick_person
Comments
Post a Comment