views:

25

answers:

1

I'm thinking about how to structure the database tables on my Ruby on Rails app. It's an app that will allow academic surveys to be sent to a student population. But as I haven't had a lot of experience with database design, I don't know the answer to the following:

Which of the following should my tables look like?

Survey
  ID
  questions (has_many)
  etc...

Questions
  ID
  question (string)
  response (has_many)

Answers
  ID
  questions (belongs_to)
  response-text (string)

or...

Survey
  ID
  questions (has_many)
  etc...

Questions
  ID
  question (string)
  responses (string, or hash, or something. Don't even know if this is possible.)

Or should I do something completely different?

A: 

surveys have questions. questions have answers

Survey
  has_many :questions
  has_many :answers, :through => :questions
end

Question
  belongs_to :survey
  has_many :answers
end

Answer
  belongs_to :question
end
Jed Schneider
So then it's OK to have a table just with just one column? (or 4, really, but only one used practically)
Reactor5
Given that you don't opt out, tables usually get id, created_at, modified_at column. belongs_to means the table gets association_id column. Apart from that if that were me, Survey would have a name, question text and answer text minimally. All in all much more than just one column table.
Hugo
you dont define the columns in the activerecord model classes. you define the relationships. in order to hook up the above ar classes, you need to have a survey_id column on the question table, and a question_id on the answer table. the other data attributes will be evident as you build your app.
Jed Schneider