# Using standard validation methods in custom validation metho

**URL:** <https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050>\
**Category:** Rails\
**Created:** [May 15, 2009, 10:15am UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050 "2009-05-15T10:15:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 15, 2009, 10:15am UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/1 "2009-05-15T10:15:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 15, 2009, 10:47am UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/2 "2009-05-15T10:47:35Z")

</div>

On May 15, 9:15 am, Jim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 15, 2009, 11:24am UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/3 "2009-05-15T11:24:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 15, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/4 "2009-05-15T15:53:11Z")

</div>

On 15 May 2009, at 10:24, Jim B. 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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 15, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/5 "2009-05-15T22:56:53Z")

</div>

> you can even shorten that to
> 
> validates\_numericality\_of :children, :if =\> Proc.new { |applicant|  
> applicant.children !="" }

Cool!  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 17, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/6 "2009-05-17T16:38:16Z")

</div>

Jim B. 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](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.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 17, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/7 "2009-05-17T16:57:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 17, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/8 "2009-05-17T17:13:49Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [May 17, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/using-standard-validation-methods-in-custom-validation-metho/168050/9 "2009-05-17T17:07:34Z")

</div>

Jim B. 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
