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.
3d0ecdd006b20274400d883dc4d4dda7?d=identicon&s=25 Juan Tarquino (juantar)
on 2006-05-25 22: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.
119af50160cabfe1fb6f2f05f5018c64?d=identicon&s=25 James Ludlow (Guest)
on 2006-05-25 22:58
(Received via mailing list)
On 5/25/06, Juan Tarquino <jptarqu@yahoo.com> wrote:
>     end
That logic looks backwards.

unless (quantity > -2147483648 && quantity < 2147483648)
  ...
end
6c27f78ab0eee78732ae54e8b8718b84?d=identicon&s=25 David Felstead (Guest)
on 2006-05-26 02: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.