如何检查用户是否为名人投票 Rails 4?

How to check if a user has voted for a celebrity Rails 4?

根据我的要求,我需要为所有名人投票,但如果我为特定名人投票,则应该不允许在 24 小时内为同一名人投票。

Vote.rb

class Vote < ActiveRecord::Base
attr_accessible :celebrity_id, :user_id

belongs_to :user
belongs_to :celebrity, counter_cache: true
end

Celebrity.rb

class Celebrity < ActiveRecord::Base
attr_accessible :name, :gender, :category_id, :image, :votes_count
validates_presence_of :name
belongs_to :user
belongs_to :category
has_many :votes
end

我的控制器:

def vote
@celebrities = Celebrity.find(params[:id])
if current_user.votes.present?
  if current_user.votes.last.updated_at < Time.now - 24.hours
    @vote = current_user.votes.build(celebrity_id: @celebrities.id, :id => params[:vote])        
    @vote.save
  end
  respond_to do |format|
    format.html { redirect_to ranking_screen_url }
    format.json { render json: @vote, status: :created }
  end
else
  @vote = current_user.votes.build(celebrity_id: @celebrities.id, :id => params[:vote])     
  @vote.save
  respond_to do |format|
    format.html { redirect_to ranking_screen_url }
    format.json { render json: @vote, status: :created }
  end
end
end

而不是检查 current_user.votes.present?,我需要检查用户是否已投票给 celebrity_id,投票数 table。有人可以帮我吗?

if current_user.votes.present?

此处您正在检查用户是否进行了任何投票。我认为您应该获取特定名人的用户投票。喜欢

@celebrity = Celebrity.find(params[:id])
@vote = current_user.votes.where(celebrity_id: @celebrity.id).first

if @vote
  if @vote.updated_at < (Time.now - 24.hours)
    # update count here
  else
else

end