如何每天验证一个帖子? [英] How do I validate one post per day?

查看:85
本文介绍了如何每天验证一个帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个验证,以确保每天一个帖子,从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

这篇关于如何每天验证一个帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆