Forum: Ruby on Rails validates_numericality_of, :allow_nil => true?

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.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-03 21:16
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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-03 23:15
(Received via mailing list)
Jeff Coleman wrote:
> Is there a way to use validates_numericality_of and still allow null
> values?

:allow_nil => true

--

Ray
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-03 23:24
(Received via mailing list)
Ray Baxter wrote:
> Jeff Coleman 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: http://tinyurl.com/fnl3e

--

Ray
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-04 00:05
(Received via mailing list)
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 Baxter" <ray@warmroom.com>
To: <rails@lists.rubyonrails.org>
Sent: 2006-04-03 4:22 PM
Subject: [Rails] Re: validates_numericality_of, :allow_nil => true?
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-04 00:28
Jeff Coleman 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
This topic is locked and can not be replied to.