How do I validate one post per day?

后端 未结 2 1614
不知归路
不知归路 2021-01-24 02:53

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 whe

相关标签:
2条回答
  • 2021-01-24 03:12

    That error is because today is instance method of model, not a scope.

    What you need is scope:

     scope :today, lambda{ where(:created_at => (Time.now.beginning_of_day..Time.now)) }
    
    0 讨论(0)
  • 2021-01-24 03:23

    You shoud use scopes for 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

    0 讨论(0)
提交回复
热议问题