简体   繁体   中英

Ruby on Rails - The way to define answers is true / false in model Answer

I'm building an online test app. In my app, one question can be a True/False question, Single choice (only one answer is accepted), Multi choice (many answers are accepted). I create models:

class Question < ActiveRecord::Base
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end  

The Question model will have a question_type_id to check it is a T/F, Single or Multi choice.

The Answer model will have a column called content typed text, and i will have a boolean column called correct to know which answer is accepted. With single and multi choice questions, i think answers are text will be fine to store in content column, but with True/False question, is it good idea if i only store answers is text like 'True', 'False' in content column and set a True for answer is accepted in correct column? I don't know another better way to deal with True/False question, can anybody help me?

Storing the boolean values as strings seems like the most reasonable way to accomplish what you want. I would recommend normalizing the values on save so that true and false always look the same when you fetch an answer.

Since I have done this kind of app before I can help you....

First of all you should differentiate between the different kinds of questions in your database...Suppose we give Ids to them then we have

0 for T/F Questions 1 for Single Correct MCQ 2 for Multiple Correct MCQ

Once you have done this, you need to have a different model named as Options,

Question has many options and Question has one answer through options

In the option model, we should 4 columns assuming (option A,B,C,D) it is a four choice questions...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM