On Nov 23, 2009, at 3:08 PM, señor tower wrote:
> Guys - newbie here with big question - at least for me is big :)
> (pardon if i dont refer to things with the proper terms)
>
> I have a controller here that lets a user update their status
> infinitively but what I want to do is
> limit them to say only update their status 5 times a day... meaning
> every 24 hours, so if they update their status 5 times
> they are blocked from doing so but then after 24 hours have past, they
> are again allowed to post for 5 times again...
So if I update my status on Monday at 00:05, 06:00, 12:00, 18:00, 23:59
Can I update my status Tuesday at 00:01? I think that your answer  
should be "no, because you have 5 updates within 24 hours."
However, if I try to update Tuesday at 00:10, I think that it should  
be OK because only the updates from Monday at 6:00, 12:00, 18:00, and  
23:59 have occurred within 24 hours.
So let me rephrase your restriction (as I understand it):
     An update is only allowed if there are fewer
     than 5 updates in the past 24 hours.
Now, given your code in PostsController, I''m assuming that you
can''t
really update a status, but instead you create a post. (Presumably the  
post indicates one''s status, right?)
So that sounds like:
class Post < ActiveRecord::Base
   belongs_to :user
   validates_presence_of :user_id
   before_create :not_too_frequently
   protected
   def not_too_frequently
     recent = self.class.count(:conditions => [''user_id = ? AND  
created_at > ?'',
                                               self.user_id,  
24.hours.ago])
     recent < 5
   end
end
You''ll notice that I''m also assuming that there is a user_id
column
since you said "lets a user update their status" and somewhere that  
means:
class User
   has_many :posts
end
> if anyone can help get me on the right track, that will be great.
> thanks
> señor.
>
> class PostsController < ApplicationController
>  def index
>    @posts = Post.all(:order => "created_at DESC")
>    respond_to do |format|
>      format.html
>    end
>  end
>
>  def create
>    @post = Post.create(:message => params[:message])
>    respond_to do |format|
>      if @post.save
>        format.html { redirect_to posts_path }
>        format.js
>      else
>        flash[:notice] = "Message failed to save."
>        format.html { redirect_to posts_path }
>      end
>    end
>  end
> end
>
> --
You might also want to do something like:
   self.errors.add_to_base("Only 5 status posting per 24 hours")
and then in the controller:
   flash[:notice] = "Message failed to save:
#{@post.errors.on[:base]}"
You could even get really fancy and tell the user how long until a new  
post would be accepted.
-Rob
Rob Biedenharn		http://agileconsultingllc.com
Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=.