Validates_numericality_of, :allow_nil => true?

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

Jeff C.man wrote:

Is there a way to use validates_numericality_of and still allow null
values?

:allow_nil => true

Ray

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

Ray

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.” [email protected]
To: [email protected]
Sent: 2006-04-03 4:22 PM
Subject: [Rails] Re: validates_numericality_of, :allow_nil => true?

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