6

I'm trying to limit user's posts to once a day, I was thinking about checking if Time.now - last_post_time is < (second in a day) but then that would force a 24hour period between each post.

What'd I'd like to do is just allow one post per day in the month, so if user posts on the 28th of march he cannot post again until the 29th. But if he posted at 10pm on march 28th he could post again at 12:01am on march 29th.

How would I do this?

Edit:

Here is my posts_controller, can I get some help on how to refactor 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 was trying something like this but it certainly 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
trying_hal9000
  • 4,343
  • 8
  • 43
  • 62

2 Answers2

5

I would probably add this check in a validation in the Post model. Perhaps 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
DanneManne
  • 21,107
  • 5
  • 57
  • 58
  • Thank you for the help also, I have tried this solution as is but I get an error when trying to access the user's homepage "ArgumentError (You need to supply at least one validation):app/models/post.rb:34:in `' app/models/post.rb:19:in `' app/controllers/pages_controller.rb:6:in `home' – trying_hal9000 Mar 29 '11 at 04:40
  • Sorry, typo by me. Should be validate and not validates – DanneManne Mar 29 '11 at 04:43
  • This is working now, thank you. I see that the redirect_to and render methods are unavailable for some reason, is there a way render pages#home with the error? – trying_hal9000 Mar 29 '11 at 04:57
  • When you say the they are unavailable, where did you put them? The beauty of validations is that if errors are added in the model then the @post.save in your controller will return false so you can handle all render or redirect there and not in the model. – DanneManne Mar 29 '11 at 05:04
2
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 all in all, 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
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
  • Thanks for the response, do you think you could elaborate a little about that code. I'm not very familiar with SQLite code and it looks like that is what you are using? This is very much appreciated. – trying_hal9000 Mar 29 '11 at 04:05
  • FYI, if you use DAY instead of DATE it will prevent the users to post the same day the next month. Don't know if that was the purpose but I don't think so – DanneManne Mar 29 '11 at 04:27
  • Thank you so much for the explanation, while I understand now what you have written, I'm still stumped on how to factor it into my code. I've edited the original question to have my post_controller create method, this is where I assume I need to refactor. Could you please shed some light for me on how to integrate this solution? – trying_hal9000 Mar 29 '11 at 04:39