Forum: Ruby on Rails Using standard validation methods in custom validation metho

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-15 10:15
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-15 10:47
(Received via mailing list)
On May 15, 9:15 am, Jim Burgess <rails-mailing-l...@andreas-s.net>
wrote:
> 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
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-15 11:24
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-15 15:53
(Received via mailing list)
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
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-15 22:56
> you can even shorten that to
>
> validates_numericality_of :children, :if => Proc.new { |applicant|
> applicant.children !="" }
>

Cool!
Thanks a lot.
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2009-05-17 16:38
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/Val...

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.
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-17 16:57
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?
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2009-05-17 17:07
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
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-17 17:13
> 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.
This topic is locked and can not be replied to.