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

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?

Ryan -

Thanks for the response.

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_equal_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

Does anyone have anything on this issue?