0
:doctor has_many :appointments
:doctor has_many :patients, through: :appointments

:appointment belongs_to :doctor
:appointment belongs_to :patient

:patient has_many :appointments
:patient has_many :doctors, through: :appointments

:appointments 有三列,:patient_id, :doctor_id, :location。现在对于特定的医生,我可以使用一个查询创建一个患者及其相关预约

doctor.patients.create!(name: "John Smith")

但是,这会使相关的 :appointment 中的 :location 列等于 nil。有没有办法在上面的命令中指定 :location ?我想只用一个查询创建患者和相关约会(并指定:位置)。那可能吗?我试图猜测正确的解决方案,如下所示,但没有奏效:

doctor.patients.create!(name: "John Smith", appointment: {location: "hospital"})
4

1 回答 1

1

尝试:

class Patient < ActiveRecord::Base
  accepts_nested_attributes_for :appointments
end

并且您的代码应该只需进行少量更改即可:

doctor.patients.create!(name: "John Smith", appointments_attributes: {location: "hospital"})
于 2013-09-30T12:23:24.153 回答