Topic: Questionnaire App

Hi.

I'm doing an app to support questionnaires. Basically I've questions, replies and users.

One problem is I have different kind of replies: boolean replies (Someone says yes or no to the question), integer replies (In question we ask about about the grade of something), text replies (Fill the name of your company or so), complex questions (1-Do you agree, 2-Relevance of this question, ...), ... but only one kind of question (text question) How could I manage it?

Other problem is how could I show questions to the user (I think it's not a good idea to manage them by id), grouped by sections (It could be seen as groups of questions). E.G.: We have questions 1,2,3,4. We want a new question 5 to be seen between 2 and 3, not after 4.

And last question is how could I see to the view I want some questions grouped in a box, other grouped in a table with a common head, an so on.

Thanks in advance,
VPC

Re: Questionnaire App

Questions Table:

id | question | answer_type | answer_id
1  | who is this? | text    |  1
2  | 2 + 2 = 5 | bool       |  1

Boolean_Answers Table:

id | answer
1  |  true

Text_Answers Table:

id | answer
1  | YO MOMMA!

Re: Questionnaire App

Couldn't it be a better idea to do same with replies?
I say:

Answers Table:

id | type | answerBool | answerText | answerInt
1  | bool | true       |            |
2  | text |            | YO MOMMA!  |
3  | int  |            |            | 4567

I think previous idea can't support referencial integrity of DB.
Is this a good idea or it could be better previous idea?

Thanks
VPC

Re: Questionnaire App

Either way would work, it's up to you what you find easier to work with I'd say. The one table solution would end up holding a lot of redundant (empty) fields. jbartel's idea would need a polymorphic connection which may be overkill for this relatively simple application.

If you go your way you could make do with a Single Table Inheritance solution, like this:

  class Answer < ActiveRecord::Base
    def result
      nil
    end
  end
  class BoolAnswer < Answer
    def result
      self.answerBool
    end
  end
  class TextAnswer < Answer
    def result
      self.answerText
    end
  end
  class IntegerAnswer < Answer
    def result
      self.answerInt
    end
  end

Then all you'd have to do to get an answer is
  @a = Answer.find :first
  @a.result # <- this returns the result of any answer, regardless what type it is.

If you went with jbartel's approach you'd end up with something more like this:
  @a = Answer.find :first
  @a.content.answer # <- assuming your polymorphic connection is called "content"

Re: Questionnaire App

i would go single table inheritance myself. It is true you create a lot of empty fields in the DB, but the code ends up a lot cleaner. That's usually my #1 priority (whether it should be or not smile )

Re: Questionnaire App

Yea I think you guys are right about the single table being better for this app. My idea came from a much more complex application we're working on, yes the polymorphic connections are a bit of a pain.

Re: Questionnaire App

OK, so I should use polymorphic connection.

I have two ideas, one is reply depends on question and that's the only relation between questions and replies. The other is reply depends on question but reply_bool depends on question_bool, reply_int on question_int and so on.

I think second is not a good idea, so rails has a type field on reply model to know what kind of reply it has, but I'm not sure.

How could it be done better?

On the other hand, every time I show questions I wanted to show which questions user has replied and which he has not replied yet, so I could write some question class methods like Questions.replied and Questions.not_replied, but how could I mix both of them when I show them?

And how will rails know a reply has boolean or integer or other type? If I use a Question method, it will know them as Questions, Am I wrong?