Sessions in Rails

I’m sorry, I originally posted this topic on Rails-Deployment, but it’s
evidently the wrong area.
I didn’t want to, did that on distraction, sorry :slight_smile:

Anyway, the original post was this below:

Hello!
I’m at my very first application in Rails, and I’m learning from Agile
Development with Rails.

I’m developing a web app that is run as a questionary.
The idea is to create a database of questions (like 50), but use only
like 10-15 each session, for each user.
You can start the questionary only if you give your email
address(validatates_presence_of).

The point I’m wondering about is : since the answers are multiples and
the sessions too, how can I keep track about whose belong certains
answers? Shall I haven’t a pool of mixed answers?
I was thinking about a migration with 16 slots (email, and 15 slots for
each answer), could that work? I know it’s not exactly in the DRY logic,
but in this way I’ll have a complete table, with which I could also
calculate how many correct answer the user did, and statistics stuff
like that.

On Wednesday, May 25, 2011 8:53:14 AM UTC-6, Ruby-Forum.com User wrote:

Welcome!

each answer), could that work? I know it’s not exactly in the DRY logic,
but in this way I’ll have a complete table, with which I could also
calculate how many correct answer the user did, and statistics stuff
like that.

Have you considered a structure such as:

tables:
questions (id, …)
answers (id, question_id, result_id, …)
results (id, email, …)

With…

class Question < ActiveRecord::Base
has_many :answers
end

class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :result
end

class Result < ActiveRecord::Base
has_many :answers
end

With this setup, you’d create a Result once a user gives you a valid
email
address (this could include checking if there is already a “result” from
that email address). Then, your main questionnaire form loads a random
set
of Question instances (10-15) and, using them, renders your form(s) to
ask
them. As responses come in, you create/validate Answer instances that
are
linked to the current Result (represents the client’s questionnaire
session)
and the proper Question.

Thanks very much! I’ll mess around with the code and I’ll let you know
:slight_smile:

I’ve got an issue :how can I pick datas from a controller to another
controller’s view?

For example, how can I show a Question in the Answer’s index.html.erb?

If i write
<%= @questions.question %> on Answer’s index.html.erb, it doesn’t
recognize the method, even if I previously wrote on the Question’s
model.

hmmm not really sure if I understand your question but here goes…

class AnswerController < ApplicationController
def index
@questions = Question.find(:all)
end
end

If you have this code on your controller then the reason why it doesn’t
recognize your method is because @questions here is an Array of
Questions. So you have to loop through your array and display each
question.

So i think you have to do this on your index.html.erb

<% @questions.each do |q| %>
<%= q.question %>
<% end %>

It was this, so the method index had to be copied exactly how it is to
another controller in order to can list another controller’s array.
Right!

Thank you very much :slight_smile: