让我们以关联文档中的示例为例:
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
我的问题是,假设Appointment
模型有一个appointment_date
字段,当与新医生和患者一起工作时,我如何才能最优雅地设置它?
现在我的创建解决方案是,而不是使用:
physician = Physician.new(...)
patient = Patient.new(...)
physician.patients < patient
physician.save
我用:
physician = Physician.new(...)
patient = Patient.new(...)
appointment = Appointment.new
appointment.physician = physician
appointment.patient = patient
appointment.appointment_date = Time.now
appointment.save
对于查询,我找到了以下解决方案。
有没有更短/更简洁/“Rails”的方式?