ActiveRecord 调查模型 ActiveRecord Associations for Doctor -> Patient
ActiveRecord Survey Model ActiveRecord Associations for Doctor -> Patient
我正在尝试构建一个调查表,以便用户可以 select 一个调查模板,然后将呈现一个包含特定于该模板的问题的表单。
class Survey < ActiveRecord::Base
belongs_to :template
belongs_to :patient
has_many :questions, :through=> :template
has_many :answers, :through=> :questions
end
class Template < ActiveRecord::Base
has_many :surveys
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :template
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :survey
end
问题 table 包含每个模板附带的 30 个预置问题列表:
create_table "questions", force: :cascade do |t|
t.integer "template_id"
t.text "content"
t.string "field_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "category"
t.string "options"
t.boolean "additional"
end
acl = Template.create(name:"ACL", body_part:"knee")
acl.questions.create(content:"Diagnosis", field_type:"text_area", category: "S")
我可以致电 Patient.surveys 获取与患者相关的所有调查的列表。我可以调用 Patient.surveys.first.questions 来获取与给定调查相关的问题列表。
但我的困境是我无法弄清楚如何获得与特定调查的特定问题相关的答案。因为按照现在的设置,每个问题都有来自许多不同调查的多个答案。
理想情况下,我可以致电 Patient.surveys.first.questions.first.answer 以获得该调查那个问题的具体答案。
但是现在,我需要去 Patient.surveys.first.questions.first.answers.where(survey_id: Survey.first.id)
所以我的问题是:
我需要在我的联想中调整什么,以便我可以打电话:
Patient.surveys.first.questions.first.answer 以获得与问题和调查相关的正确答案?
嗯,首先要注意的是,您正在对实际上可以弹出到一个程序范围内的内容执行多个查询。我不知道您需要什么大局,所以我可能完全不知道什么对您更重要。
要具体回答您的问题,您可以定义一个获取正确答案的问题方法。
def answer
survey=Survey.joins(:templates).where(:templates {id: self.template_id, question_id: self.id}).first
Answer.where(:survey_id => survey.id).first
end
然后就像
那样称呼它
patient.surveys.first.questions.first.answer
您也可以通过将其弹出到您的答案模型中来进行范围界定以直接获得答案
scope :for_survey_question, lambda {|survey, question| where(:survey_id => survey.id, :question_id => question.id).first }
并在任何地方调用它作为 Answer.for_survey_question(survey,question)(其中 survey 和 question 都是调查和问题模型对象,分别作为参数加载和传递)
我正在尝试构建一个调查表,以便用户可以 select 一个调查模板,然后将呈现一个包含特定于该模板的问题的表单。
class Survey < ActiveRecord::Base
belongs_to :template
belongs_to :patient
has_many :questions, :through=> :template
has_many :answers, :through=> :questions
end
class Template < ActiveRecord::Base
has_many :surveys
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :template
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :survey
end
问题 table 包含每个模板附带的 30 个预置问题列表:
create_table "questions", force: :cascade do |t|
t.integer "template_id"
t.text "content"
t.string "field_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "category"
t.string "options"
t.boolean "additional"
end
acl = Template.create(name:"ACL", body_part:"knee")
acl.questions.create(content:"Diagnosis", field_type:"text_area", category: "S")
我可以致电 Patient.surveys 获取与患者相关的所有调查的列表。我可以调用 Patient.surveys.first.questions 来获取与给定调查相关的问题列表。
但我的困境是我无法弄清楚如何获得与特定调查的特定问题相关的答案。因为按照现在的设置,每个问题都有来自许多不同调查的多个答案。
理想情况下,我可以致电 Patient.surveys.first.questions.first.answer 以获得该调查那个问题的具体答案。
但是现在,我需要去 Patient.surveys.first.questions.first.answers.where(survey_id: Survey.first.id)
所以我的问题是:
我需要在我的联想中调整什么,以便我可以打电话:
Patient.surveys.first.questions.first.answer 以获得与问题和调查相关的正确答案?
嗯,首先要注意的是,您正在对实际上可以弹出到一个程序范围内的内容执行多个查询。我不知道您需要什么大局,所以我可能完全不知道什么对您更重要。
要具体回答您的问题,您可以定义一个获取正确答案的问题方法。
def answer
survey=Survey.joins(:templates).where(:templates {id: self.template_id, question_id: self.id}).first
Answer.where(:survey_id => survey.id).first
end
然后就像
那样称呼它patient.surveys.first.questions.first.answer
您也可以通过将其弹出到您的答案模型中来进行范围界定以直接获得答案
scope :for_survey_question, lambda {|survey, question| where(:survey_id => survey.id, :question_id => question.id).first }
并在任何地方调用它作为 Answer.for_survey_question(survey,question)(其中 survey 和 question 都是调查和问题模型对象,分别作为参数加载和传递)