Joey Cheng
Joey Cheng

Reputation: 1

How to limit the number of votes in 'acts_as_votable' gem

I'm currently using the rubygem acts_as_votable as the voting system in my project, but I want to limit the total votes of every user (which I used rubygem devise)

The target of the vote is a model called Pin:

class Pin < ActiveRecord::Base
   acts_as_votable 
end

Should I use a method and put it in the before_action: to make sure that the vote your making won't let your total votes exceed say like 10?

Updated: 8/18/2015

Now I popped up with a new question: I created another model group, and declaim the relationship:

(group.rb)

has_many: pins

(pin.rb)

belongs_to: group

So, here comes up the question, if I want to limit the votes in every group, say like: 10 in group 1, 10 in group 2, 10 in group 3....

How can I accomplish it?

Upvotes: 0

Views: 190

Answers (1)

Gagan Gami
Gagan Gami

Reputation: 10251

You can do something like this:

def upvote
  @pin = Pin.find(params[:id])
   # check for user's total votes
  if current_user.find_voted_items.size < 10
    @pin.vote_by :voter => current_user
  else
    ..... #your code
    flash[:notice] = "your total votes exceed"
    redirect_to pins_path
  end
end

Upvotes: 1

Related Questions