在rails 4中通过到期更新列

时间:2015-09-08 05:09:56

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

用户应该只在24小时内为名人投票一次。所以我需要更新用户表中的投票列。我在这里做的是我正在检查当前用户是否已投票。如果不是,我允许他投票。

我的模特

vote.rb

attr_accessible :celebrity_id, :user_id
belongs_to :user
belongs_to :celebrity, counter_cache: true

user.rb

attr_accessible :email, :name, :uid, :provider, :vote
has_many :votes

celebrity.rb

attr_accessible :name, :gender, :category_id, :image, :votes_count  
belongs_to :user
belongs_to :category
has_many :votes

我的控制器

@celebrities = Celebrity.find(params[:id])    
@vote = current_user.votes.build(celebrity_id: @celebrities.id, :id => params[:vote])    
respond_to do |format|
  if current_user.vote != true
    current_user.vote = true
    current_user.save
    @vote.save        
    format.json { render json: @vote, status: :created }
  else        
    format.json { render json: @vote.errors, status: :unprocessable_entity}
  end
end

但是,与投票表created_at time相比,如何将用户表中的投票列设置为每24小时为零

0 个答案:

没有答案