ruby on rails - How do I validate one post per day? -
i trying create validation ensure 1 post per day, 24hrs starting 00:00. how can done in rails please?
i did following not sure put today
method. simpler alternatives appreciated.
def today where(:created_at => (time.now.beginning_of_day..time.now)) end
i added validation article model:
validate :time_limit, :on => :create
and defined time_limit
in same model so:
def time_limit if user.articles.today.count >= 1 errors.add(:base, "exceeds daily limit") end
but keep getting "no method" error in create action.
undefined method `today'
i'm not sure put method.
you shoud use scopes that:
class article scope :today, -> { where(:created_at => (time.now.beginning_of_day..time.now.end_of_day)) } end
http://apidock.com/rails/activerecord/namedscope/classmethods/scope
Comments
Post a Comment