Jim Burgess
2009-May-15 08:15 UTC
Using standard validation methods in custom validation metho
Hi, Is it possible to use standard validation methods in custom validation methods? eg: def check_children if children != "" validates_numericality_of :children end end When I run this code I get a no method error: undefined method `validates_numericality_of'' for #<Applicant:0x5d1bba4> Is there any way around this? Thanks in advance -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-May-15 08:46 UTC
Re: Using standard validation methods in custom validation metho
On May 15, 9:15 am, Jim Burgess <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > Is it possible to use standard validation methods in custom validation > methods? > eg: > > def check_children > if children != "" > validates_numericality_of :children > end > end > > When I run this code I get a no method error: > > undefined method `validates_numericality_of'' for #<Applicant:0x5d1bba4> > > Is there any way around this? >Not really. validates_numericality_of is a class method that defines an appropriate validation on a class. You may find the :allow_nil and :if or :unless options useful Fred> Thanks in advance > -- > Posted viahttp://www.ruby-forum.com/.
Jim Burgess
2009-May-15 09:24 UTC
Re: Using standard validation methods in custom validation metho
Wow, that''s very cool. I changed the line to: validates_numericality_of :children, :if => Proc.new { |applicant| true if applicant.children !="" } and that does exactly what I want. Thanks for your time and help, Fred. -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-May-15 13:52 UTC
Re: Using standard validation methods in custom validation metho
On 15 May 2009, at 10:24, Jim Burgess wrote:> > Wow, that''s very cool. > I changed the line to: > > validates_numericality_of :children, :if => Proc.new { |applicant| > true > if applicant.children !="" }you can even shorten that to validates_numericality_of :children, :if => Proc.new { |applicant| applicant.children !="" } Fred> > > and that does exactly what I want. > Thanks for your time and help, Fred. > > -- > Posted via http://www.ruby-forum.com/. > > >
Jim Burgess
2009-May-15 20:56 UTC
Re: Using standard validation methods in custom validation metho
> you can even shorten that to > > validates_numericality_of :children, :if => Proc.new { |applicant| > applicant.children !="" } >Cool! Thanks a lot. -- Posted via http://www.ruby-forum.com/.
Stephan Wehner
2009-May-17 14:38 UTC
Re: Using standard validation methods in custom validation metho
Jim Burgess wrote:> >> you can even shorten that to >> >> validates_numericality_of :children, :if => Proc.new { |applicant| >> applicant.children !="" } >> >Looks like a case for allow_nil: validates_numericality_of :children, :allow_nil => true The documentation at http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000091 mentions :allow_nil - Skip validation if attribute is nil (default is false). Notice that for fixnum and float columns empty strings are converted to nil. Stephan> Cool! > Thanks a lot.-- Posted via http://www.ruby-forum.com/.
Jim Burgess
2009-May-17 14:57 UTC
Re: Using standard validation methods in custom validation metho
Thanks for your reply> Looks like a case for allow_nil: > > validates_numericality_of :children, :allow_nil => trueI created the field ''children'' as a string (can''t remember why, perhaps because someone might enter ''none''??), so that doesn''t work. Is there any big advantage to changing the field ''children'' into an interger? -- Posted via http://www.ruby-forum.com/.
Stephan Wehner
2009-May-17 15:07 UTC
Re: Using standard validation methods in custom validation metho
Jim Burgess wrote:> Thanks for your reply > >> Looks like a case for allow_nil: >> >> validates_numericality_of :children, :allow_nil => true > > I created the field ''children'' as a string (can''t remember why, perhaps > because someone might enter ''none''??), so that doesn''t work. > > Is there any big advantage to changing the field ''children'' into an > interger?Well, if the column will hold numbers sure there is an advantage. You can perform arithmetic and numeric comparisons. You can sort based on the natural order of numbers, not based on lexicographical order. Stephan -- Posted via http://www.ruby-forum.com/.
Jim Burgess
2009-May-17 15:13 UTC
Re: Using standard validation methods in custom validation metho
> Well, if the column will hold numbers sure there is an advantage. You > can perform arithmetic and numeric comparisons. You can sort based on > the natural order of numbers, not based on lexicographical order.Yeah, that''s true enough. I''ll get it changed as I want to do everything properly. Thanks for your help. -- Posted via http://www.ruby-forum.com/.