Hello, What is the best way to validate date periods? I have a model which has two date attributes, and the user is not able to create a new record if date1 is greater than date2. I tried using validates_numericality_of but it doesn''t work with dates. -- Posted via http://www.ruby-forum.com/.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=UTF-8" http-equiv="Content-Type"> <title></title> </head> <body bgcolor="#ffffff" text="#000000"> Eduardo Bueno wrote: <blockquote cite="mid:377f65fd470a148c56197f9b2110d5fe-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org" type="cite"> <pre wrap="">Hello, What is the best way to validate date periods? I have a model which has two date attributes, and the user is not able to create a new record if date1 is greater than date2. I tried using validates_numericality_of but it doesn''t work with dates. </pre> </blockquote> <font size="-1"><font face="Helvetica, Arial, sans-serif">I created my own validator for dates checking for the correct order and for valid dates. I check valid dates by creating and catchng the exception if the date is invalid.<br> <br> <br> </font></font> <br> --~--~---------~--~----~------------~-------~--~----~<br> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. <br> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en<br> -~----------~----~----~----~------~----~------~--~---<br> </body> </html> <br>
michael.hasenstein-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2009-Oct-16 16:24 UTC
Re: Date validation
Create your own validation method like this: http://guides.rubyonrails.org/activerecord_validations_callbacks.html#creating-custom-validation-methods> What is the best way to validate date periods? I have a model which has > two date attributes, and the user is not able to create a new record if > date1 is greater than date2. I tried using validates_numericality_of but > it doesn''t work with dates.
irb(main):001:0> require ''date'' => true irb(main):002:0> d1 = Date.new(2009, 4, 1) => #<Date: 2009-04-01 (4909845/2,0,2299161)> irb(main):003:0> d2 = Date.new(2009, 5, 1) => #<Date: 2009-05-01 (4909905/2,0,2299161)> irb(main):004:0> d3 = Date.new(2009, 6, 1) => #<Date: 2009-06-01 (4909967/2,0,2299161)> irb(main):005:0> d2.between? d1, d3 => true On Oct 16, 12:24 pm, "michael.hasenst...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <michael.hasenst...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Create your own validation method like this: > > http://guides.rubyonrails.org/activerecord_validations_callbacks.html... > > > What is the best way to validate date periods? I have a model which has > > two date attributes, and the user is not able to create a new record if > > date1 is greater than date2. I tried using validates_numericality_of but > > it doesn''t work with dates.