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". -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 12:23, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 13:38, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 19 June 2011 12:23, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 13:18, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 14:26, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 => trueIt''s just the same. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 13:38, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 19 June 2011 12:23, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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_landSame problem, I don''t know how to insert nil values for the two fields. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 18:04, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 13:43, Bill Felton <subscriptions-WQJLJidLLO77x2Bgvr0FtA@public.gmane.org> wrote:> > 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".That''s ok, they are numeric fields I thought that allow_nil should be ok. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 19, 2011, at 2:43 PM, Mauro wrote:> On 19 June 2011 13:43, Bill Felton <subscriptions-WQJLJidLLO77x2Bgvr0FtA@public.gmane.org> wrote: >> >> 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". > > 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 June 2011 21:26, Bill Felton <subscriptions-WQJLJidLLO77x2Bgvr0FtA@public.gmane.org> wrote:> > On Jun 19, 2011, at 2:43 PM, Mauro wrote: > >> On 19 June 2011 13:43, Bill Felton <subscriptions-WQJLJidLLO77x2Bgvr0FtA@public.gmane.org> wrote: >>> >>> 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". >> >> 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?allow_blank works. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Apparently Analagous Threads
- validates_numericality_of, :allow_nil => true?
- validates uniqueness scope allow_blank/allow_nil -> validation error
- :allow_nil validation vs. blank?
- validates_file_format_of only when is there an image
- validates_numericality_of with greater_than* less_than* simply don't work