如何每天验证一个帖子?

问题描述:

我想要创建一个验证,以确保每天一个帖子,从00:00开始24小时。

I am trying to create a validation to ensure one post per day, 24hrs starting from 00:00. How can this be done in Rails please?

我做了以下操作,但我不知道今天放在哪里方法。非常感谢更简单的替代品。

I did the following but I am not sure where to put the today method. Simpler alternatives are much appreciated.

def today
  where(:created_at => (Time.now.beginning_of_day..Time.now))
end



然后我对文章模型添加了一个验证:

I then added a validation to the article model:

validate :time_limit, :on => :create

并在同一个文件中定义 time_limit 模型如下:

and defined time_limit in the same model like so:

def time_limit
 if user.articles.today.count >= 1
 errors.add(:base, "Exceeds daily limit")
end

在创建操作中获取无方法错误。

But I Keep getting a "no method" error in the create action.

undefined method `today'

我不确定在哪里放这个方法。

I'm not really sure where to put this method.

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