ruby on rails - rails3 user can only vote once per day -


class rate < activerecord::base  attr_accessible :image_id, :rate, :user_id  belongs_to :image  belongs_to :user  validate :user_can_rate_after_one_day   before_save :default_values  def default_values  self.rate ||=0 end  protected   def user_can_rate_after_one_day     r=rate.where(:image_id =>image_id, :user_id=> user_id).order("created_at desc").limit(1)      if( (time.now - 1.day) < r[0].created_at)       self.errors.add(:rate,"you can vote once per day")      else       return     end   end end 

i have 1 rate model, , want user can rate once per day. write user_can_rate_after_one_day method validte it. if delete function, user can rate many time, if add function, user can not rate it. knows what's wrong here? thanks


Popular posts from this blog

Php - Delimiter must not be alphanumeric or backslash -

trac - Modifying workflow to add a status and split on ticket type -

Delphi interface implements -