Search code examples
ruby-on-railsrubyruby-on-rails-3voting

Why doesn't this Ruby on Rails voting code work?


So I really thought this would work to create the voting system like we have here on SO:

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

  if @vote.value.nil?
    if params[:type] == "up"
      @vote.value = 1
    else
      @vote.value = -1
    end
  elsif (params[:type] == "up" && @vote.value == 1) || (params[:type] == "down" && @vote.value == -1)
    @vote.value = 0
  elsif ((params[:type] == "up" && @vote.value == -1) || (params[:type] == "down" && @vote.value == 1)) || (@vote.value == 0)
    if params[:type] == "up"
      @vote.value = 1
    else
      @vote.value = -1
    end
  end  

  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

I tried to follow the example of the first answer in this question: Why doesn't this Ruby on Rails code work as I intend it to? My code, however, doesn't let me vote on a video for the first time because of this error:

TypeError (nil can't be coerced into Fixnum):
app/models/video_vote.rb:11:in `update_vote_sum'
app/controllers/video_votes_controller.rb:4:in `create

Here's my videovote model:

class VideoVote < ActiveRecord::Base
  belongs_to :user
  belongs_to :video
  validates_uniqueness_of :user_id, :scope => :video_id

  after_create :update_vote_sum

  private

    def update_vote_sum
      video.update_attributes!(:vote_sum => video.vote_sum + value)
    end
end

And in case this is needed, here's my vote_sum method from my Video model:

def vote_sum
  video_votes.sum(:value)
end

Solution

  • In your after_create method in VideoVote.rb, you're summing over all votes, including the one you've just created with a nil value. Switch after_create to after_update, or set a default for value on VideoVote.

    In looking at that, if vote_sum is calling sum on all of your votes every time anyways, you probably don't even need an after_create or after_update method if votes have a default value set.

    --

    You could also replace this entire system with the thumbs_up gem, and save yourself some hassle.