ccuilla
2008-May-12 22:58 UTC
Has anyone had problems with validates_numericality_of and :greater_than?
I cannot seem to get this to validate correctly and wonder if I''m doing something wrong. I''m using Rails 2.0.2 and I have been trying to take advantage of the "validates_numericality_of" with the ":greater_than" option with something like this: validates_numericality_of :users, :greater_than => 0, :only_integer => true, :allow_nil => true, :message => "The number of allowed users must be a whole number greater than zero." But this doesn''t seem to be catching values set to zero or negative values. Am I using this correctly? NOTE: I have the same problem with the "greater_than_or_equal_to" option so I assume it is something generally I''m doing wrong. validates_numericality_of :value, :greater_than_or_equal_to => 0, :only_integer => true, :allow_nil => true, :message => "Cannot use a negative or fractional 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-May-12 23:39 UTC
Re: Has anyone had problems with validates_numericality_of and :greater_than?
Did you set the field to be an integer? If so, then you shouldn''t need the :only_integer option specified. Also, how is the users value being passed in? Are you trying to validate the presence of, rather than the numericality of the users, or is users automatically set to 0 as the default in your table? Have you tried taking :allow_nil out? On Tue, May 13, 2008 at 8:28 AM, ccuilla <ccuilla-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I cannot seem to get this to validate correctly and wonder if I''m > doing something wrong. > > I''m using Rails 2.0.2 and I have been trying to take advantage of the > "validates_numericality_of" with the ":greater_than" option with > something like this: > > validates_numericality_of :users, :greater_than => 0, :only_integer > => > true, :allow_nil => true, :message => "The number of allowed users > must be a whole number greater than zero." > > But this doesn''t seem to be catching values set to zero or negative > values. Am I using this correctly? > > NOTE: I have the same problem with the "greater_than_or_equal_to" > option so I assume it is > something generally I''m doing wrong. > > validates_numericality_of :value, :greater_than_or_equal_to => > 0, :only_integer => true, :allow_nil => true, :message => "Cannot use > a negative or fractional number." > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Cuilla
2008-May-13 00:53 UTC
Re: Has anyone had problems with validates_numericality_of and :greater_than?
Ryan - Thanks for the response. On May 12, 2008, at 5:39 PM, Ryan Bigg (Radar) wrote:> Did you set the field to be an integer?Set it how? In the database? In the DB is in an INT.> If so, then you shouldn''t need the :only_integer option specified.Fair enough, except that these validations are at the model level and, I assume, without them, my user could enter a fractional number and then have ActiveRecord silently convert that to an INT at save time, whereas I''d like a more proactive validation message given to them if they should try this.> Also, how is the users value being passed in?Well, currently I''m doing this in unit tests.> Are you trying to validate the presence of, rather than the > numericality of the users, or is users automatically set to 0 as > the default in your table?No default value at the database level.> Have you tried taking :allow_nil out?Yes, but...two things: 1) that doesn''t help, and 2) I want to allow nil for this value, but when the value is provided, I want this greater_than validation to apply, which seems like a valid scenario Here''s the full model: class ServicePlan < ActiveRecord::Base has_many :accounts validates_presence_of :name, :monthly_service_charge validates_length_of :name, :maximum => 255 validates_uniqueness_of :name, :case_sensitive => false validates_numericality_of :monthly_service_charge, :greater_than_or_equa l_to => 0, :message => "TBD" validates_numericality_of :count_sheets_per_week, :greater_than => 0, :only_integer => true, :allow_nil => true, :message => "TBD" validates_numericality_of :users, :greater_than => 0, :only_integer => true, :allow_nil => true, :message => "TBD" validates_numericality_of :report_recipients, :greater_than => 0, :only_integer => true, :allow_nil => true, :message => "TBD" validates_numericality_of :templates, :greater_than => 0, :only_integer => true, :allow_nil => true, :message => "TBD" end --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ccuilla
2008-May-15 00:48 UTC
Re: Has anyone had problems with validates_numericality_of and :greater_than?
Does anyone have anything on this issue? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---