# "Quiz" site

**URL:** <https://www.ruby-forum.com/t/quiz-site/93427>\
**Category:** Rails\
**Created:** [April 2, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/quiz-site/93427 "2006-04-02T22:25:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 2, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/quiz-site/93427/1 "2006-04-02T22:25:04Z")

</div>

Hi,

I am thinking about how to best design a quiz site in RoR. I think it  
should be fairly easy, but have run into a wall. In the simplest form,  
the view would grab all of the questions for a particular quiz and  
display them (e.g. multiple choice, so 4 option buttons per question).  
However, I can’t figure out how to “mark” the quiz. The form is  
submitted to the controller, which must somehow work out whether each  
one is correct. Perhaps each option group could have some form of  
identifier that the controller looks at, makes a new “question” object  
and checks “question.correct\_answer” or somesuch? Once this is done,  
the users profile can be updated with their result (etc).

Now if I want to display questions in a random order (say a quiz has 40  
questions, I only want 20 for a particular attempt), I can easily grab  
only 20 and jumble them up. But how does the controller know how to  
mark them? Is the idea above of having the identifier sufficient here?

As a final question, I was considering different types of question. For  
example, a multiple choice question (so one row per option), a question  
where the user must enter a number/string (one row with the correct  
answer). The view could add a ‘type’, but checking whether a number  
answer is actually a number would suddenly come down to the controller,  
not the model. Unless of course I use different tables for different  
types of answer (so different models), or there is some sort of OOo foo  
with the model that can distinguish and have different  
validates\_something tests.

It seems that suddenly the application is not only a data-bound  
input/output site, and I’m stuck 🙂

David

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 3, 2006, 2:02am UTC](https://www.ruby-forum.com/t/quiz-site/93427/2 "2006-04-03T02:02:40Z")

</div>

David wrote:

> Hi,
> 
> I am thinking about how to best design a quiz site in RoR. I think it  
> should be fairly easy, but have run into a wall. In the simplest form,  
> the view would grab all of the questions for a particular quiz and  
> display them (e.g. multiple choice, so 4 option buttons per question).  
> However, I can’t figure out how to “mark” the quiz. The form is  
> submitted to the controller, which must somehow work out whether each  
> one is correct. Perhaps each option group could have some form of  
> identifier that the controller looks at, makes a new “question” object  
> and checks “question.correct\_answer” or somesuch? Once this is done,  
> the users profile can be updated with their result (etc).

How about an Answer table? The Answer table would have a question\_id  
column, and a correct\_answer column.

class Question \< ActiveRecord  
has\_one :answer

# returns true if the given answer is correct

def is\_correct?(guess)  
guess == correct\_answer  
end

end

class Answer \< ActiveRecord  
belongs\_to :question  
end

The correct\_answer could by just a varchar type to hold ‘A’, ‘B’, ‘C’,  
or ‘D’, or whatever you want to use.

You could then easily have a Quiz table that has\_many :questions, and  
off you go.

Make sense?

Jeff

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 3, 2006, 7:00am UTC](https://www.ruby-forum.com/t/quiz-site/93427/3 "2006-04-03T07:00:17Z")

</div>

create database “localhost:quiz\_development”  
PAGE\_SIZE=8192 length = 1024 pages  
DEFAULT CHARACTER SET ISO8859\_1;  
commit;

create table userids  
(  
ID char(36) not null primary key,  
name varchar (255)  
);

create table presentations  
(  
ID char(36) not null primary key,  
textvalue varchar (1024),  
audio varchar (1024),  
visual varchar (1024)  
);

create table quizzes  
(  
ID char(36) not null primary key,  
name varchar (255) not null,  
preamble\_presentation\_id char(36),  
postamble\_presentation\_id char(36),  
foreign key (preamble\_presentation\_id) references presentations(ID),  
foreign key (postamble\_presentation\_id) references presentations(ID)  
);

create table questions  
(  
ID char(36) not null primary key,  
PARENT\_ID char(36) not null,  
presentation\_id char(36) not null,  
seq integer not null,

foreign key (PARENT\_ID) references quizzes(ID),  
foreign key (presentation\_id) references presentations(ID)  
);  
create unique ascending index quest\_seq on questions (PARENT\_ID,SEQ);

create table answers  
(  
ID char(36) not null primary key,  
PARENT\_ID char(36) not null,  
presentation\_id char(36) not null,  
seq integer not null,  
iscorrect char (1) default ‘N’ not null,

foreign key (PARENT\_ID) references questions(ID),  
foreign key (presentation\_id) references presentations(ID)  
);  
create unique ascending index seq on answers (PARENT\_ID,SEQ);

create table quizruns  
(  
ID char(36) not null primary key,  
quiz\_id char(36) not null,  
user\_id char(36) not null,  
started TIMESTAMP not null,  
finished TIMESTAMP,  
asked integer not null,  
answered integer not null,  
correct integer not null,

foreign key (quiz\_id) references quizzes(ID),  
foreign key (user\_id) references userids(ID)  
);

create table answerruns  
(  
ID char(36) not null primary key,  
quizrun\_id char(36) not null,  
started TIMESTAMP not null,  
finished TIMESTAMP,  
answer\_id char(36),

foreign key (quizrun\_id) references quizruns(ID),  
foreign key (answer\_id) references answers(ID)  
);

grant all on userids to quiz;  
grant all on presentations to quiz;  
grant all on quizzes to quiz;  
grant all on questions to quiz;  
grant all on answers to quiz;  
grant all on quizruns to quiz;  
grant all on answerruns to quiz;  
commit;
