Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement one vote per user per comment?

I currently have a comment controller that has the method vote_up and vote_down this is how my vote_up currently works.

My Comment model has description and a count field.

  def vote_up
    @comment = Comment.find(params[:comment_id])
    @comment.count += 1
    if @comment.save
      flash[:notice] = "Thank you for voting"
      respond_to do |format|
        format.html { redirect_to show_question_path(@comment.question) }
        format.js
      end
    else
      flash[:notice] = "Error Voting Please Try Again"
      redirect_to show_question_path(@comment.question)
    end
  end

This allows for multiple vote up and downs. How would I design it so that a user can only vote once per comment but somehow keep track if they voted up or down, so they have the ability to change their vote if they want too.

like image 438
Kevin Avatar asked Jul 21 '11 13:07

Kevin


1 Answers

You could do something like this. It prohibits identical votes but allows changing the vote to the opposite (it's a thumbs up/thumbs down system).

def vote(value, user) # this goes to your model

  #find vote for this instance by the given user OR create a new one
  vote = votes.where(:user_id => user).first || votes.build(:user_id => user)

  if value == :for
    vote_value = 1
  elsif value == :against
    vote_value = -1
  end

  if vote.value != vote_value
    vote.value = vote_value
    vote.save
  end
end

migration:

def self.up
    create_table :votes do |t|
    t.references :comment, :null => false
    t.references :user, :null => false
    t.integer :value, :null => false
  end
  add_index :votes, :post_id
  add_index :votes, :user_id
  add_index :votes, [:post_id, :user_id], :unique => true
end

Alternatively, you could use a gem called thumbs_up or any other.

like image 137
bassneck Avatar answered Oct 10 '22 18:10

bassneck