用户应该只在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小时为零