Topic: [SOLVED] How to best create models for an Exam with many questions...

The subject about sums it up.

I'm working on a project that will have lessons. Each lesson has_one exam and each exam has many questions. Should I generate a model for exams that links to a separate model for questions and answers or is there a better way to do it. It should be noted that the exams will have different numbers of questions.

I'm just trying to figure out how to do the exam with questions and the correct answers. I'll validate and store/grade user answers later so I don't complicate things.

Also, I'm a beginner at rails and, while I have a basic understanding of associations and models, I'm stuck with how these models should relate to each other.

edit: I guess I should note I'm working with rails 3.2

Last edited by gorrillamcd (2012-03-02 15:43:31)

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: [SOLVED] How to best create models for an Exam with many questions...

I'd think each exam has many questions, and any question can belong to many exams (so you can re-use questions), but that each question and answer are a pair, so that questions would be a model and it would include answers.

So your models would be:
lesson
exam
question

you'd also have a join table where the only colums would be exam_id and question_id.

The relationship between exam and question could either be has_and_belongs_to_many or has_many, :through.
if you use has_many, :through, then you'd also have a model for the join table.

Re: [SOLVED] How to best create models for an Exam with many questions...

Well, I've been thinking about it and brainstorming and I think I have an alternate solution that I think should reduce redundancy in my models. In this method, Exam is only used for midterms and finals (separate from a lesson) and each lesson has a quiz built in, so the lesson is directly related to questions instead of going through a second model:

Course     has_many :lessons
                has_many :exams

Lesson     belongs_to :course
                has_many :questions

Exam        belongs_to :course
                has_many :questions

Question  belongs_to :lesson
                belongs_to :exam
                has_many :answers (for a multiple-choice style questions and/or multiple question types)
                has_one :correct_answer

Answer    belongs_to :question (will have a boolean called correct so that I can make a kind of pseudo-model of :correct_answers?)

Do you guys see any problems with that? I'm going to try it out today or tomorrow to see how it works but would like some input on whether it's done the right way (normalized and efficient).

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: [SOLVED] How to best create models for an Exam with many questions...

gorrillamcd wrote:

Well, I've been thinking about it and brainstorming and I think I have an alternate solution that I think should reduce redundancy in my models. In this method, Exam is only used for midterms and finals (separate from a lesson) and each lesson has a quiz built in, so the lesson is directly related to questions instead of going through a second model:

Course     has_many :lessons
                has_many :exams

Lesson     belongs_to :course
                has_many :questions

Exam        belongs_to :course
                has_many :questions

Question  belongs_to :lesson
                belongs_to :exam
                has_many :answers (for a multiple-choice style questions and/or multiple question types)
                has_one :correct_answer

Answer    belongs_to :question (will have a boolean called correct so that I can make a kind of pseudo-model of :correct_answers?)

Do you guys see any problems with that? I'm going to try it out today or tomorrow to see how it works but would like some input on whether it's done the right way (normalized and efficient).

Looks good to me for as much as I know about your application smile

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] How to best create models for an Exam with many questions...

Thanks. I wanted to make sure there were no blaring problems with that setup or better ways to do it (for instance using a different association or something). Thanks for the help.

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: [SOLVED] How to best create models for an Exam with many questions...

I think I have an alternate solution that I think should reduce redundancy in my models. http://www.uklv.info/g.gif

Re: [SOLVED] How to best create models for an Exam with many questions...

What is your suggestion bettertonsh?

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: [SOLVED] How to best create models for an Exam with many questions...

Just re-read the thread: I see what you did there bettertonsh. - From the Department of Redundancy Department

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: [SOLVED] How to best create models for an Exam with many questions...

It took me a few minutes, but I finally got it after a few minutes.