Forum: Ruby on Rails Best way to validate an integer field?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Juan T. (Guest)
on 2006-05-26 00:48
I am trying to validate an integer field called quantity. I don't know
how to chack if it has gone beyond or below the maximum or minimum value
allowed by the Firebird RDBMS. I tired adding this code to the validate
method in the model:

if (quantity > -2147483648) and (quantity < 2147483648)

        errors.add("quantity", "is out of range (must be between
-2147483648 and 2147483648)")

    end

But when I try entering a big value for example a bunch of numbers in
the form field I get this sql error when I hit edit:

FireRuby::FireRubyException: Error preparing a SQL statement.
Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 239
9
Invalid token
SQL Code = -104
Firebird Code = 335544569
: UPDATE parts SET "ORDERED_DATE" = CAST('2006-05-24 21:45:00' AS
TIMESTAMP), "ORDER_ID" = 13, "END_DATE" = NULL, "PROVIDER_ID" = 2,
"CODE" = 'jojo                     ', "GOAL_DATE" = CAST('2006-05-28
21:45:00' AS TIMESTAMP), "QUANTITY" =
99999999999999999999999999999999999999999999888888888888888888888888888888886666666666666666666666
WHERE id = 2

Any suggestions on the best way to validate an integer field?

Best regards.
James L. (Guest)
on 2006-05-26 00:58
(Received via mailing list)
On 5/25/06, Juan T. <removed_email_address@domain.invalid> wrote:
>     end
That logic looks backwards.

unless (quantity > -2147483648 && quantity < 2147483648)
  ...
end
David F. (Guest)
on 2006-05-26 04:45
(Received via mailing list)
In your model class:

validates_inclusion_of :quantity, :in => -2147483648..2147483648

Cheers!

-DF
This topic is locked and can not be replied to.