Re: Validating DateHelpers date_select

John Kopanas
Thu, 26 May 2005 13:52:50 -0000

I think I am making things more complicated then need be for the time
being.

In the User model I just added:

     def before_save
         if Time.now - birthdate < 18.years
          errors.add("birthdate", "must be over 18")
         end
     end

but fore some reason before_save is not being called before_save...
or should I be declaring before_save in another file?

Do I need to specify object before errors like User.errors.add?

Your guys help is greatly appreciated!

On 26-May-05, at 9:06 AM, John Kopanas wrote:

> Wow... thanks... makes sense.  But I am really new to both rails
> and ruby and have a couple questions about your solution if you
> don''t mind.
>
> Using the combination of what you wrote and:
> http://wiki.rubyonrails.com/rails/show/
> HowToAddCustomValidationKeywords
>
> I want to make a validate_older_then_18 so I can write in my model:
>
> validate_older_then_18 :birthdate
>
> Where would I extend the ActiveRecord::Base class?  What file do I
> put this stuff in?
>
> Thanks my friend.
>
>
> On 26-May-05, at 8:20 AM, Dee Zsombor wrote:
>
>
>> Just add a  validate member to the ActiveRecord::Base decendant class
>> that has birthdate field. Inside chegk if they are 18 or above. If
>> they are not add a desciptive message to the errors field. Something
>> roughly looking like:
>>
>> def validate
>>    if Time.now - birthdate < 18.years
>>     errors.add("birthdate", "must be over 18")
>>    end
>> end
>>
>> Zsombor
>>
>> On 5/26/05, John Kopanas
<john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> wrote:
>>
>>
>>> I use date_select on my sign_up sheet for people to choose their
>>> birthdate.  For people to join the website they have to be over 18.
>>> How would I go about validating that in a rails way.  For all the
>>> other validations I got them.  I just can''t figure out how
to do
>>> this
>>> one yet.
>>>
>>> Any suggestions? :-)
>>> _______________________________________________
>>> Rails mailing list
>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>>
>>>
>>>
>>
>>
>> --
>> http://deezsombor.blogspot.com
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails