2011-10-18 102 views
1

我創建一個aplication,用戶可以有兩種類型:一個病人或醫生。每種類型都有其自己的一組屬性。Rails的:模型繼承

是否有可能創建一個包含共享的屬性的用戶模型,然後創建一個從用戶繼承病人和醫生的模型?

回答

2

沒有,但你可以做你說什麼,然後爲每個子類的關聯添加到包含特定屬性的模型。然後你可以使用delegate使事情顯得渾然一體。

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 
+0

謝謝!它解決了我的問題。 – aperez