lundi 7 septembre 2015

Update column through expiry in rails 4

An user should vote for a celebrity only once in 24 hours. So I need to update a votes column in user table. What I am doing here is that I am checking whether the current user has voted. If not I am allowing him to vote.

My models are

vote.rb

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

user.rb

has_many :votes

celebrity.rb

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

My controller

@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

But, how can I set vote column in user table to zero every 24 hrs comparing to votes table created_at time

Aucun commentaire:

Enregistrer un commentaire