# Validates\_numericality\_of, :allow\_nil =\> true?

**URL:** <https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/93471>\
**Category:** Rails\
**Created:** [April 3, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/93471 "2006-04-03T21:16:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 3, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/93471/1 "2006-04-03T21:16:21Z")

</div>

Is there a way to use validates\_numericality\_of and still allow null  
values?

I have a model with some optional values that can be nil, but if they’re  
present, they must be numbers. Will I need to construct a custom  
valiation for this, or is there some method built in? Seems like it  
would be a common enough need.

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 3, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/93471/2 "2006-04-03T23:15:21Z")

</div>

Jeff C.man wrote:

> Is there a way to use validates\_numericality\_of and still allow null  
> values?

:allow\_nil =\> true

–

Ray

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 3, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/93471/3 "2006-04-03T23:24:43Z")

</div>

Ray B. wrote:

> Jeff C.man wrote:
> 
> > Is there a way to use validates\_numericality\_of and still allow null  
> > values?
> 
> :allow\_nil =\> true

So I see my own post and yours have :allow\_nil =\> true in the title, so  
maybe I misunderstood the question.

class Song \< ActiveRecord::Base  
validates\_numericality\_of :length\_in\_seconds, :allow\_nil =\> true  
…  
end

Details here: [Peak Obsession](http://tinyurl.com/fnl3e)

–

Ray

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 4, 2006, 12:05am UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/93471/4 "2006-04-04T00:05:02Z")

</div>

This may not be at all relevent to your problem, but the way I  
understand  
it, ‘nil’ and ‘null’ are not the same thing.

hth  
Bill  
----- Original Message -----  
From: “Ray B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: 2006-04-03 4:22 PM  
Subject: [Rails] Re: validates\_numericality\_of, :allow\_nil =\> true?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 4, 2006, 12:28am UTC](https://www.ruby-forum.com/t/validates-numericality-of-allow-nil-true/93471/5 "2006-04-04T00:28:05Z")

</div>

Jeff C.man wrote:

> Is there a way to use validates\_numericality\_of and still allow null  
> values?
> 
> I have a model with some optional values that can be nil, but if they’re  
> present, they must be numbers. Will I need to construct a custom  
> valiation for this, or is there some method built in? Seems like it  
> would be a common enough need.
> 
> Jeff

That will teach me to post without trying it. :allow\_nil wasn’t listed  
in the attributes for validates\_numericality\_of in Agile Web Dev, so I  
just assumed and posted prematurely. It works fine.

Jeff
