I need to make a recommendation questionnaire, where answering a bunch of questions would lead to some unique result; decisions along the way should affect the end result.
It's fine to make it very basic - so I have two models - Questions
and Answers
. Each question has two answers, and each of the answers leads to one other question - so graphing out the hieararchy ends up like sort of like a binary tree:
Question 1
/ \
Answer 1 Answer 2
/ \
Question 2 Question 3
/ \ / \
Ans3 Ans4 Ans5 Ans6
...
So in plain english I'd say that each question has many
answers, and each answer has one
parent question and has one
child question.
For a typical has many
relationship, with the little experience I have I would probably just put the foreign key on the many
end and not worry about the one
end; so here Answer
would get a question_id
which doesn't have to be unique.
For has_one
relationship, either end of relationship seems fine - but I already have a question_id
on the answer; so maybe putting "parent" answer's answer_id
on the question is appropriate, but that seems mighty confusing lexically - looking up something like Question.answer
I would not intuitively understand it's the response that led to this question...
Alternatively, I could have two question_ids
on in the Answer
model - but then would have to differentiate between which one is "parent question" and which one is "follow-up question"...
Another consideration is that when I fetch the "next question" from the database (based on the answer), I also want to get the possible answers to display. Although optimizing the performance is not a requirement, it would be nice to "do it right" (or at least not completely wrong).
What's the best approach?