: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
:约会有三列,:patient_id,:doctor_id,:location。现在,对于特定的医生,我可以使用
创建患者及其相关的约会doctor.patients.create!(name: "John Smith")
然而,这会使相关的约会中的:location列等于nil。有没有办法在上面的命令中指定:location?我想用一个查询创建患者和相关的约会(并指定:位置)。那可能吗?我尝试猜测正确的解决方案,如下所示,但它不起作用:
doctor.patients.create!(name: "John Smith", appointment: {location: "hospital"})
答案 0 :(得分:1)
尝试:
class Patient < ActiveRecord::Base
accepts_nested_attributes_for :appointments
end
您的代码应该只需要很小的改动:
doctor.patients.create!(name: "John Smith", appointments_attributes: {location: "hospital"})
答案 1 :(得分:0)