2

I am working on a multiple choice question and answer application using Ruby on Rails and I have the following model.

class User < ActiveRecord::Base
  has_many :questions
end  

class Question < ActiveRecord::Base
  belongs_to :user
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
  has_many :votes
end

class Vote < ActiveRecord::Base
  belongs_to :user
  belongs_to :answer
end

My problem is a user can choose all the answers.

How can I fix it so that it updates to the new answer?

For example, a has 5 votes and b 3 votes.

User clicks on a and a increments to 6 votes, same user comes back and clicks on b, a decrements back to 5 votes and b increments to 4 votes.

My first guess is that I need to add another model for example, user_choice with user_id and vote_id to track the previous answer.

Steve Robinson
  • 3,759
  • 3
  • 36
  • 57
Francis Chibaye
  • 125
  • 1
  • 1
  • 10

1 Answers1

2

You have your answer within the models. Just add a point system to the question model.

Under the question model,

def points
  self.answers.count
end

Or, if answers have different values attached to them, make points an attribute of the answer instances.

def points
  self.answers.pluck(:points).inject(:+)
end

This will sum up all the points from an answer belonging to a question, which allows you to order the questions by point count.

However, I am assuming you will need a Choice model for a question, unless that is in fact what the votes are for.

question has_many choices

choices belong_to question, etc.

I'm not sure what you mean when you say:

How can I fix it so that it updates to the new answer?

EDIT

Er, okay so you just need exactly what I mentioned earlier. A vote is really just the number of times an answer has been chosen.

If your problem is that users can choose more than 1 answer, it is likely that you should implement view based validations via javascript, or better yet, just disable their ability to select multiple choices, which if you were using a select tag, is actually the default. You must've added

multiple: true

in the select tag options for the user to select multiple entries.

On the backend, you can do this:

class Choice < ActiveRecord::Base
validates_uniqueness_of :user_id, scope: [:question_id, :answer_id]
end
Community
  • 1
  • 1
Sherwyn Goh
  • 1,352
  • 1
  • 10
  • 19
  • I gave an example. Answers will display as links and users can only pick one answer like any multiple choice question – Francis Chibaye Jan 30 '14 at 04:57
  • I am using links not select tags. – Francis Chibaye Jan 30 '14 at 06:04
  • you should be using a form to submit it, with ajax maybe. http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html – Sherwyn Goh Jan 30 '14 at 06:24
  • my main problem is how prevent user from choosing all the answers. – Francis Chibaye Jan 31 '14 at 04:04
  • your main problem is you are using a link to do something a form should be doing, link_to is really only suitable if you are doing something like changing a state, or a boolean variable – Sherwyn Goh Jan 31 '14 at 12:51
  • I found a gem that does something similar to what I am looking for, the [robut-quiz](https://github.com/burtio/robut-quiz) only it's a sinatra gem. You know how facebook questions worked when user post a question and friends respond by clicking on either yes or no and it returns the number of vote for each. I want something like that only user provide custom options for the questions. I really appreciate your taking time this and I hope you understand what I am trying to achieve. many thanx. – Francis Chibaye Feb 01 '14 at 04:21