[英]Rails association has_many through not working with multiple models
I am a newbie to rails. 我是护栏的新手。 I wanted to try out has_many through association with the following example. 我想通过与以下示例关联来尝试has_many。
Relation: Operation_doctors can only make an appointment with operation patients and vice versa. 关系:手术医生只能预约手术患者,反之亦然。 Same applies for treatment doctors. 同样适用于治疗医生。
Doctor.rb Doctor.rb
class Doctor < ActiveRecord::Base
has_many :appointments
has_many :operation_patients, :class_name => 'Patient', :source => :operation_patient ,through: :appointments
has_many :treatment_patients, :class_name => 'Patient', :source => :treatment_patient ,through: :appointments
end
Patient.rb Patient.rb
class Patient < ActiveRecord::Base
has_many :appointments
has_many :operation_doctors,:class_name => 'Doctor', :source => :operation_doctor,through: :appointments
has_many :treatment_doctors,:class_name => 'Doctor', :source => :treatment_doctor,through: :appointments
end
Appointment.rb 约会
class Appointment < ActiveRecord::Base
belongs_to :operation_doctor, :class_name => 'Doctor', :foreign_key => :operation_doctor
belongs_to :treatment_doctor, :class_name => 'Doctor', :foreign_key => :treatment_doctor
belongs_to :operation_patient, :class_name => 'Patient', :foreign_key => :operation_patient
belongs_to :treatment_patient, :class_name => 'Patient', :foreign_key => :treatment_patient
end
Error in Rails console: Input: Rails控制台中的错误:输入:
@op1 = Patient.new
@od1 = Doctor.new
Error: 错误:
irb(main):023:0> @op1.operation_doctors << @od1
(0.1ms) begin transaction
(0.1ms) rollback transaction
ActiveRecord::UnknownAttributeError: unknown attribute 'patient_id' for Appointment.
In patient.rb
在patient.rb
class Patient < ActiveRecord::Base
has_many :appointments
For this to work, you need a patient_id
field on the Appointment table. 为此,您需要在“约会”表上创建一个patient_id
字段。 Your actual appointment relation is more complex. 您的实际约会关系更加复杂。 There are 2 ways for resolving the error. 有两种方法可以解决该错误。
class Patient < ActiveRecord::Base
has_many :operation_appointments, :class_name => 'Appointment', :foreign_key => :operation_patient
has_many :treatment_patients, :class_name => 'Appointment', :foreign_key => :treatment_patient
You will need to do the same thing for the doctor's model. 您将需要为医生的模型做同样的事情。
I would be inclined to make a larger datamodel for this solution. 我倾向于为此解决方案制作一个更大的数据模型。 Since you know that doctors have two types and patients have two types, they are incompatible with each other, and they have logic that would be duplicate, I would lean on inheritance. 既然您知道医生有两种类型,患者有两种类型,它们彼此不兼容,并且它们具有可以重复的逻辑,所以我将依靠继承。
Here's a sketch of my data model: 这是我的数据模型的草图:
ActiveRecord::Base
Doctor Patient Appointment
OperationDoctor OperationPatient OperationAppointment
TreatmentDoctor TreatmentPatient TreatmentAppointment
The Appointment table would contain a doctor_id, patient_id, and type field 约会表将包含doctor_id,Patient_id和类型字段
rails g model appointment type:string patient:references doctor:references
rails g model operation_appointment --parent appointment
This would simplify the classes: 这将简化类:
# appointment.rb
belongs_to :patient
belongs_to :doctor
# patient.rb
has_many :appointments
has_many :doctors, :through => :appointment
This way your doctor would either be a TreatmentDoctor or a OperationDoctor, and you can use validation on those models to ensure the patient is of the correct type. 这样,您的医生将成为TreatmentDoctor或OperationDoctor,并且您可以对这些模型进行验证以确保患者的类型正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.