我目前正在使用rubygem acts_as_votable 作为我项目中的投票系统,但我想限制每个用户的总投票数(我使用rubygem { {3}} )
投票的目标是一个名为Pin
的模型:
class Pin < ActiveRecord::Base
acts_as_votable
end
我应该使用method
并将其放在before_action:
中,以确保您所做的投票不会让您的总票数超过10吗?
更新时间:2015年8月18日
现在我提出了一个新问题:
我创建了另一个模型group
,并声明了关系:
(group.rb)
has_many: pins
(pin.rb)
belongs_to: group
所以,这里提出了一个问题,如果我想限制每个组中的投票,比如:第1组10个,第2组10个,第3组10个......
我怎样才能完成它?
答案 0 :(得分:1)
您可以这样做:
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