Validates_numericality_of, :allow_nil => true?


#1

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


#2

Jeff C.man wrote:

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

:allow_nil => true

Ray


#3

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: http://tinyurl.com/fnl3e

Ray


#4

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


#5

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