ruby on rails - Proper Setup of Active Record Associations for a Doctor-Patient Survey Model -
i having trouble building active record associations doctor -> patient relationship.
doctors can create assessments patients. before create assessment, must choose template (for type of injury). template has_many :questions, , question has_many :answers.
so models are: user, patient, assessment, template, question, answer.
the user --> patient relationship pretty straight forward, i'm having trouble the template, assessment, questions, , answers. i'm pretty confused 'has_many :through'. i'd able call template.questions list of questions given template, also able call assessment.questions (instead of assessment.template.questions).
then can filter through assessment.questions answers.
here current model associations. current setup isn't allowing me call assessment.questions (which thought taken care of has_many :questions, :through=> :templates).
what need change in order call assessment.questions ? accepting other feedback on architecture.
thanks
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
personally, way. may not understanding goal, think do:
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
Comments
Post a Comment