2015-05-29 33 views
0

我在爲醫生建立活動記錄關聯時遇到了一些麻煩 - >患者關係。爲醫生 - 患者調查模型正確設置活動記錄關聯

醫生可以爲病人評估。但是在創建評估之前,他們必須選擇一個模板(針對傷害的類型)。模板has_many:問題和has_many問題:答案。

所以型號有:用戶,病人,評估,模板,提問,回答。

用戶 - >醫患關係是非常簡單的,但我在使用的模板,評估,問題和答案的麻煩。我對'has_many:through'很困惑。我希望能夠調用Template.questions得到的給定模板的問題之列,但也能夠調用Assessment.questions(而不是Assessment.template.questions)。

然後,我可以通過Assessment.questions過濾,以獲得答案。

這裏是我目前的模型協會。目前的設置不允許我打電話給Assessment.questions(我認爲這會被has_many:questions,:through =>:templates所照顧)。

我需要做什麼才能叫Assessment.questions改變?還接受關於該體系結構的任何其他反饋。

感謝

class User < ActiveRecord::Base 
    has_many :patients 
    has_many :assessments, dependent: :destroy 
end 

class Patient < ActiveRecord::Base 
    belongs_to :user 
    has_many :assessments, dependent: :destroy 
end 

class Assessment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :template 
    belongs_to :patient 
    has_many :questions, :through=> :templates 
    has_many :answers, :through=> :questions 
    accepts_nested_attributes_for :answers 
end 

class Template < ActiveRecord::Base 
    belongs_to :assessment 
    has_many :questions 
end 

class Question < ActiveRecord::Base 
    belongs_to :template 
    has_many :answers 
end 

class Answer < ActiveRecord::Base 
    belongs_to :question 
end 
+1

您擁有belongs_to:模板評估和belongs_to:評估模板。這是一個問題。 – Noah

+0

患者通過評估有許多用戶,用戶有許多患者通過評估聽起來正確。你沒有連貫地解釋項目的目標或領域,以便讓我很好地理解模型應該如何相互關聯。 – Noah

+0

謝謝@Noah。這是醫生管理患者名單並進行評估的簡單工具。我有一個觀點來展示所有患者,並且患者可以進行多項評估,這些評估都顯示在他們的個人資料中。雖然患者仍然可以不經評估仍然存在 –

回答

1

就個人而言,我會做這種方式。我可能不完全瞭解您的目標,但我認爲我應該這樣做:

class Doctor < ActiveRecord::Base 
    has_many :assessments 
    has_many :patients, :through => :assessments 
end 

class Patient < ActiveRecord::Base 
    has_many :assessments 
    has_many :doctors, :through => :assessments 
end 

class Assessment < ActiveRecord::Base 
    has_many :templates 
    belongs_to :patient 
    belongs_to :doctor 
end 

class Template < ActiveRecord::Base 
    has_many :questions 
    belongs_to :assessment 
end 

class Question < ActiveRecord::Base 
    has_many :answers 
    belongs_to :template 
end 

class Answer < ActiveRecord::Base 
    belongs_to :question 
end 
+0

然後,我會如何呈現給定患者評估的列表?我本來想打電話給@ patient.assessments查看歷史記錄 –

+1

編輯完成後,您可以執行Patient.first.assessments。 – Noah

+0

我的兩個擔憂:#1我無法打電話給Assessment.questions以獲取與該評估相關的問題的直接列表#2理想情況下,我想調用Assessment.questions.first.answer以獲取該問題的相關答案*用於相關評估* –