Ruby on rails Rails:模型继承

Ruby on rails Rails:模型继承,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我正在创建一个应用程序,其中用户可以是两种类型:患者或医生。每种类型都有自己的属性集 是否可以创建包含共享属性的用户模型,然后创建从用户继承的患者和医生模型 没有,但是您可以按照您所说的做,然后为每个子类添加一个关联到包含特定属性的模型。然后您可以使用委托使事情看起来天衣无缝 class User end class Doctor < User has_one :doctor_profile delegate :phd_in, :to => :doctor_profile

我正在创建一个应用程序,其中用户可以是两种类型:患者或医生。每种类型都有自己的属性集


是否可以创建包含共享属性的用户模型,然后创建从用户继承的患者和医生模型

没有,但是您可以按照您所说的做,然后为每个子类添加一个关联到包含特定属性的模型。然后您可以使用
委托
使事情看起来天衣无缝

class User
end

class Doctor < User
  has_one :doctor_profile
  delegate :phd_in, :to => :doctor_profile
end

class Patient < User
  has_one :patient_profile
  delegate :symptoms, :to => :patient_profile
end

class DoctorProfile
  # E.g. attributes: phd_in:string
end

class PatientProfile
  # E.g. attributes: symptoms:text
end
类用户
结束
班级医生<用户
有一个:医生简介
代表:博士学位,收件人=>:博士个人资料
结束
类患者<用户
有一个:病人档案
代表:症状,:至=>:患者档案
结束
类博士生档案
#例如属性:phd_in:string
结束
类PatientProfile
#例如属性:症状:文本
结束