How to check time in Rails? User restriction to publish once a day

I try to limit the number of user messages once a day, I was thinking about checking if Time.now is last_post_time <(second time a day), but then it will force a 24-hour period between each message.

What I would like to do is just one post per day per month, so if the user posts on March 28th, he will not be able to post the message until the 29th. But if he leaves at 22:00 on March 28, he can post a message at 12:01 on March 29.

How can I do it?

Edit:

Here is my post_controller, can I get some help on how to reorganize this?

def create @post = current_user.posts.build(params[:supportpost]) if @post.save flash[:success] = "Your post was created!" redirect_to root_path else @feed_items = [] render 'pages/home' end end 

I tried something like this, but this, of course, is wrong:

  def create post = @user.posts.find(:first, :conditions => ["STRFTIME('%d', created_at) = ?", Date.today.day]) if post @post = current_user.posts.build(params[:supportpost]) if @post.save flash[:success] = "Your post was created!" redirect_to root_path else @feed_items = [] render 'pages/home' end end 
+6
validation ruby-on-rails time ruby-on-rails-3
source share
2 answers

I would add this check in confirmation in the Post model. Maybe something like this:

 class Post < ActiveRecord::Base ... validate :date_scope private def date_scope if Post.where("user_id = ? AND DATE(created_at) = DATE(?)", self.user_id, Time.now).all.any? errors.add(:user_id, "Can only post once a day") end end end 
+5
source share
 post = @user.posts.find(:first, :conditions => ["DATE(created_at) = DATE(?)", Time.now]) if post # he made a post today! else #he can post end 

So, overall, it produces this SQL query:

  SELECT `posts`.* FROM `posts` WHERE (`posts`.user_id = 1) AND (DATE(created_at) = DATE('2011-03-29 04:35:45')) LIMIT 1 
+2
source share

All Articles