# Validates\_numercality\_of with allow\_nil

**URL:** <https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464>\
**Category:** Rails\
**Created:** [June 19, 2011, 1:25pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464 "2011-06-19T13:25:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 19, 2011, 1:25pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/1 "2011-06-19T13:25:12Z")

</div>

In the model I have:

validates :square\_meters\_public\_land, :barrier\_meters, :numericality  
=\> { :greater\_than\_or\_equal\_to =\> 0 }, :allow\_nil =\> true

but if, in the field, on create, I don’t insert a value I have the  
error “field is not a number”.

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 1:40pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/2 "2011-06-19T13:40:38Z")

</div>

On 19 June 2011 12:23, Mauro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In the model I have:
> 
> validates :square\_meters\_public\_land, :barrier\_meters, :numericality  
> =\> { :greater\_than\_or\_equal\_to =\> 0 }, :allow\_nil =\> true

Depending on your Rails version… try:

validates :square\_meters\_public\_land, :barrier\_meters, :numericality  
=\> { :greater\_than\_or\_equal\_to =\> 0 }, :if =\> Proc.new {|o|  
!o.square\_meters\_public\_land.blank?}

or

validates :square\_meters\_public\_land, :barrier\_meters, :numericality  
=\> { :greater\_than\_or\_equal\_to =\> 0 }, :if =\>  
:square\_meters\_public\_land

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 2:19pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/3 "2011-06-19T14:19:30Z")

</div>

On 19 June 2011 13:38, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 19 June 2011 12:23, Mauro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > In the model I have:
> > 
> > validates :square\_meters\_public\_land, :barrier\_meters, :numericality  
> > =\> { :greater\_than\_or\_equal\_to =\> 0 }, :allow\_nil =\> true
> 
> Depending on your Rails version… try:

I’m using rails 3.0.9, sorry but I don’t undestand why I have to use  
the if clause.  
allow\_nil =\> doens’t work with validation\_numericality\_of?

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 2:28pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/4 "2011-06-19T14:28:08Z")

</div>

On 19 June 2011 13:18, Mauro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m using rails 3.0.9, sorry but I don’t undestand why I have to use  
> the if clause.  
> allow\_nil =\> doens’t work with validation\_numericality\_of?

I didn’t say you “had to”, I suggested you try it… did you? Did it  
work?..

I doubt it anyway, as I had copied your code without checking it, and  
just added the “if” I’d copied from one of my models. I notice now  
that you’re not using “validates\_numericality\_of”, you’re using  
“validates” and seem to be trying to check two values - can’t say  
that’s an idiom I’m familiar with, but it may be valid (although I  
can’t see it in the docs:  
[http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000091](http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000091))

What about:

validates\_numericality\_of :square\_meters\_public\_land,  
:greater\_than\_or\_equal\_to =\> 0, :allow\_nil =\> true  
validates\_numericality\_of :barrier\_meters, :greater\_than\_or\_equal\_to  
=\> 0, :allow\_nil =\> true

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 7:13pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/5 "2011-06-19T19:13:00Z")

</div>

On 19 June 2011 13:38, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> { :greater\_than\_or\_equal\_to =\> 0 }, :if =\> Proc.new {|o|  
> !o.square\_meters\_public\_land.blank?}
> 
> or
> 
> validates :square\_meters\_public\_land, :barrier\_meters, :numericality  
> =\> { :greater\_than\_or\_equal\_to =\> 0 }, :if =\>  
> :square\_meters\_public\_land

Same problem, I don’t know how to insert nil values for the two fields.

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 7:05pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/6 "2011-06-19T19:05:36Z")

</div>

On 19 June 2011 14:26, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What about:
> 
> validates\_numericality\_of :square\_meters\_public\_land,  
> :greater\_than\_or\_equal\_to =\> 0, :allow\_nil =\> true  
> validates\_numericality\_of :barrier\_meters, :greater\_than\_or\_equal\_to  
> =\> 0, :allow\_nil =\> true

It’s just the same.

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 8:11pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/7 "2011-06-19T20:11:45Z")

</div>

On 19 June 2011 18:04, Mauro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > validates\_numericality\_of :barrier\_meters, :greater\_than\_or\_equal\_to  
> > =\> 0, :allow\_nil =\> true
> 
> It’s just the same.

Curious. Okay, so lastly, try:

validates\_numericality\_of :square\_meters\_public\_land,  
:greater\_than\_or\_equal\_to =\> 0, :if =\> :square\_meters\_public\_land  
validates\_numericality\_of :barrier\_meters, :greater\_than\_or\_equal\_to  
=\> 0, :if =\> :barrier\_meters

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 7:48pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/8 "2011-06-19T19:48:16Z")

</div>

On Jun 19, 2011, at 7:23 AM, Mauro wrote:

> In the model I have:
> 
> validates :square\_meters\_public\_land, :barrier\_meters, :numericality  
> =\> { :greater\_than\_or\_equal\_to =\> 0 }, :allow\_nil =\> true
> 
> but if, in the field, on create, I don’t insert a value I have the  
> error “field is not a number”.

Include the :allow\_blank option if the fields are not required.  
I generally think about this in terms of “specifying a validation always  
includes :required; if that’s not what I want, I have to include  
:allow\_blank”.

cheers,  
Bill

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 9:27pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/9 "2011-06-19T21:27:09Z")

</div>

On Jun 19, 2011, at 2:43 PM, Mauro wrote:

> > > error “field is not a number”.
> > 
> > Include the :allow\_blank option if the fields are not required.  
> > I generally think about this in terms of “specifying a validation always  
> > includes :required; if that’s not what I want, I have to include :allow\_blank”.
> 
> That’s ok, they are numeric fields I thought that allow\_nil should be ok.

I would have expected :allow\_nil to work, but apparently it’s giving  
problems? FWIW, I always tend to think in terms of :allow\_blank rather  
than :allow\_nil. Perhaps an artifact of my years in Smalltalk?  
If :allow\_nil isn’t working, as I gather from the posts that appeared  
before mine (after I posted, but long before mine showed up), have you  
tried :allow\_blank? Does that work better, worse, or no different?

regards,  
Bill

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 8:44pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/10 "2011-06-19T20:44:31Z")

</div>

On 19 June 2011 13:43, Bill F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Include the :allow\_blank option if the fields are not required.  
> I generally think about this in terms of “specifying a validation always  
> includes :required; if that’s not what I want, I have to include :allow\_blank”.

That’s ok, they are numeric fields I thought that allow\_nil should be  
ok.

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [June 19, 2011, 11:21pm UTC](https://www.ruby-forum.com/t/validates-numercality-of-with-allow-nil/208464/11 "2011-06-19T23:21:28Z")

</div>

On 19 June 2011 21:26, Bill F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > =\> { :greater\_than\_or\_equal\_to =\> 0 }, :allow\_nil =\> true  
> > > > I would have expected :allow\_nil to work, but apparently it’s giving problems?  
> > > > FWIW, I always tend to think in terms of :allow\_blank rather than :allow\_nil.  
> > > > Perhaps an artifact of my years in Smalltalk?  
> > > > If :allow\_nil isn’t working, as I gather from the posts that appeared before  
> > > > mine (after I posted, but long before mine showed up), have you tried  
> > > > :allow\_blank? Does that work better, worse, or no different?

allow\_blank works.
