Validation of Rails requires numerical strength, even if presence is not set to true

I am trying to save a record that does not have one set of fields that has a numeric check value in models. Although the presence is not required in the check, it still throws an error that the field is not a number.

Validation:

validates :network_id, :numericality => true 

Code to save the model:

 networks.each do |network| network.url = network.raw_data.link network.save! end 

Mistake:

 Validation failed: Network is not a number 
+54
ruby ruby-on-rails ruby-on-rails-3
Nov 02 2018-11-11T00:
source share
4 answers
 validates :network_id, :numericality => true, :allow_nil => true 
+106
Nov 02 2018-11-11T00:
source share
  validates :network_id, :numericality => {:allow_blank => true} 
+37
Nov 02 '11 at 1:59
source share

You must use allow_blank

 validates :network_id, :numericality => true, :allow_blank => true 
+13
Nov 02 2018-11-11T00:
source share

In Rails 4 (Ruby 2) you can write:

 validates :network_id, numericality: { greater_than_or_equal_to: 0, allow_nil: true } 
+9
Apr 7 '14 at 16:20
source share



All Articles