# Best way to validate an integer field?

**URL:** <https://www.ruby-forum.com/t/best-way-to-validate-an-integer-field/61657>\
**Category:** Rails\
**Created:** [May 25, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/best-way-to-validate-an-integer-field/61657 "2006-05-25T22:48:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![juantar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@juantar](https://www.ruby-forum.com/u/juantar)\
**Post date:** [May 25, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/best-way-to-validate-an-integer-field/61657/1 "2006-05-25T22:48:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juantar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@juantar](https://www.ruby-forum.com/u/juantar)\
**Post date:** [May 25, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/best-way-to-validate-an-integer-field/61657/2 "2006-05-25T22:58:04Z")

</div>

On 5/25/06, Juan T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```

That logic looks backwards.

unless (quantity \> -2147483648 && quantity \< 2147483648)  
…  
end

---

<div class="post-metadata">

**Author:** ![juantar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@juantar](https://www.ruby-forum.com/u/juantar)\
**Post date:** [May 26, 2006, 2:45am UTC](https://www.ruby-forum.com/t/best-way-to-validate-an-integer-field/61657/3 "2006-05-26T02:45:37Z")

</div>

In your model class:

validates\_inclusion\_of :quantity, :in =\> -2147483648…2147483648

Cheers!

-DF
