wachichornia
wachichornia

Reputation: 1188

how to handle multiple interchanging values on the client side (rails)

On the app, the user answers questions. every time he/she logs in, three unanswered questions are shown. the user may skip (or answer!) a question, then another unanswered one will show up.

I've been thinking of looking up the unanswered questions and writing a cookie with all the ids of the unanswered questions showing, and another cookie with the ids of the questions not being shown on the web page.

But i'm having a hard time figuring out how to handle the swaping of values between cookies if the user skips a question

I also thought of storing the showed questions as answered on the database, with a specific value that denotes that they are being shown but not answered yet. but that would likely be very database intensive.

I'm not looking for someone to give me the code, just point me to the most appropriate way to do this and i'll code it on my own.

I'm working on Rails.

Upvotes: 0

Views: 38

Answers (2)

Carson Cole
Carson Cole

Reputation: 4451

I'd keep it in the database for simplicity. I'd have a questions and an answers table. Here is some of the skeleton code...

user_id
question_id
answer_status

class Answer < ActiveRecord::Base
  belongs_to :user
  scope :seen_but_unanswered, where(:answer_status => 'seen')
  scope :answered, where(:answer_status => 'answered')
end

class User < ActiveRecord::Base
  has_many :answers

  def unseen_questions
    ...
  end


end

You can then make these requests:

@user.answers
@user.unseen_questions
@user.answers.seen_but_unanswered
@user.answers.answered

Upvotes: 1

Robert Chuchro
Robert Chuchro

Reputation: 127

Store all of this info in a hash and use session? This should be fine as long as the hash isn't incredibly large.

session[:unanswered] = unanswered_hash unless session[:unanswered].present?

I'm not entirely sure if this covers all you wanted to do.

Upvotes: 0

Related Questions