Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4078592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:41:37+00:00 2026-05-20T17:41:37+00:00

So I attempted to build what I asked about in this question: Fix voting

  • 0

So I attempted to build what I asked about in this question: Fix voting mechanism

However, this solution doesn’t work. A user can still vote however many times he or she wants. How could I fix this and/or refactor?

def create       
    @video = Video.find(params[:video_id])
    @vote = @video.video_votes.new
    @vote.user = current_user

    if params[:type] == "up"
      @vote.value = 1
    else
      @vote.value = -1
    end

  if @previous_vote.nil?
    if @vote.save
      respond_to do |format|
        format.html { redirect_to @video }
        format.js
      end
    else
      respond_to do |format|
        format.html { redirect_to @video }
        format.js {render 'fail_create.js.erb'}
      end
    end
  elsif @previous_vote.value == params[:type]
    @previous_vote.destroy
  else
    @previous_vote.destroy    
    if @vote.save
      respond_to do |format|
        format.html { redirect_to @video }
        format.js
      end
    else
      respond_to do |format|
        format.html { redirect_to @video }
        format.js {render 'fail_create.js.erb'}
      end
    end
  end
  @previous_vote = VideoVote.where(:video_id => params[:video_id], :user_id => current_user.id).first
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:41:38+00:00Added an answer on May 20, 2026 at 5:41 pm

    @previous_vote seems to be nil at the beginning of every request?

    I would personally do away with all the logic in the controller and put uniqueness contraints at Model or database level where they belong.

    Updated

    This is probably full of errors but treat it as pseudo-code

    Models something like:

    class Video < ActiveRecord::Base
      has_many :votes
    end
    
    class Vote < ActiveRecord::Base
      belongs_to :user
      belongs_to :video
      validates_uniqueness_of :user_id, :scope => :video_id # only one vote per person per video
    end
    
    class User < ActiveRecord::Base
      has_many :votes
    end
    

    Controller:

    def create
      @video = Video.find(params[:video_id])
      @vote = current_user.votes.find_or_create_by_video_id(@video.id)
    
      # change this next block of code so you assign value to the vote based on whatever logic you need
      if you_need_to_do_anything_to_change_its_value
        @vote.value = :whatever
      end
    
      if @vote.save
        redirect_to @video
      else
        render :whatever_is_appropriate
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempted translation of the above question from non-native English to English: This is a
I got this error when macport automatically attempted to compile it Error: Target org.macports.build
I'm attempting to build mercurial 1.3.1. I've attempted this thus far on my Mac
In this comment , user illume seems to indicate that Pygame 1.9.2 will build
My attempted solution was: $date = Nov 30 2009 03:00:00:000PM; echo date(F Y, strtotime($date));
I'm using iTextSharpText to build a PDF. I was asked to add some dynamic
I am trying to build a website with mysql and php. This is the
I'm trying to build some HTML with Knockout that Jquery UI can turn into
I attempted to build a Flex 4 skin for a Spark button, typical practice.
I recently asked a question relating to the javascript not working on my site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.