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 -- Posted via http://www.ruby-forum.com/.
Ray Baxter
2006-Apr-03 21:12 UTC
[Rails] Re: validates_numericality_of, :allow_nil => true?
Jeff Coleman wrote:> Is there a way to use validates_numericality_of and still allow null > values?:allow_nil => true -- Ray
Ray Baxter
2006-Apr-03 21:23 UTC
[Rails] Re: validates_numericality_of, :allow_nil => true?
Ray Baxter wrote:> Jeff Coleman wrote: >> Is there a way to use validates_numericality_of and still allow null >> values? > > :allow_nil => trueSo 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
Bill Walton
2006-Apr-03 22:03 UTC
[Rails] Re: validates_numericality_of, :allow_nil => true?
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?> 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 > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jeff Coleman
2006-Apr-03 22:28 UTC
[Rails] Re: validates_numericality_of, :allow_nil => true?
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. > > JeffThat 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 -- Posted via http://www.ruby-forum.com/.