Validates_numercality_of with allow_nil

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”.

On 19 June 2011 12:23, Mauro [email protected] 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

On 19 June 2011 13:38, Michael P. [email protected] wrote:

On 19 June 2011 12:23, Mauro [email protected] 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?

On 19 June 2011 13:18, Mauro [email protected] 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)

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

On 19 June 2011 13:38, Michael P. [email protected] 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.

On 19 June 2011 14:26, Michael P. [email protected] 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.

On 19 June 2011 18:04, Mauro [email protected] 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

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

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

On 19 June 2011 13:43, Bill F. [email protected]
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.

On 19 June 2011 21:26, Bill F. [email protected]
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs