# Problem with validates\_length\_of

**URL:** <https://www.ruby-forum.com/t/problem-with-validates-length-of/139392>\
**Category:** Rails\
**Created:** [June 3, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392 "2008-06-03T14:56:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 3, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/1 "2008-06-03T14:56:35Z")

</div>

I’m passing a field called :year, to the game model for a new game. It  
has  
the validation

validates\_length\_of :year, :is =\> 4, :if =\> :check\_year?

The if works (a similar validates\_numericality\_of :year) is being  
checked,  
so something before it is not working right. Why?

10 \<- passed but shouldn’t  
100 \<- passes but shoulnd’t  
1000 \<- passes and it should  
10000 \<- passes but shouldn’t

Has anyone got any ideas why this might be happening, or how to fix?

Using Rails 2.1.0.

## Thanks for your help.

Regards  
Kieran

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 3, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/2 "2008-06-03T23:08:04Z")

</div>

Its passed from a text field on a form, so technically it would be a  
string,  
but I guess it converts it on save because when I tried to concat with  
another string, it wouldn’t change types.

And I’d really rather not change it to a string and back to int just for  
one  
validation (and even if I did, I’m not sure how at this point  
(before\_save  
?)

Any other ideas?

Regards  
Kieran

On Wed, Jun 4, 2008 at 1:03 AM, Thorsten M. \<

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 3, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/3 "2008-06-03T15:03:30Z")

</div>

what type does the field have?

validates\_length\_of would work only on string or text I guess, so if  
it’s an integer it won’t work

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 3, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/4 "2008-06-03T23:23:23Z")

</div>

On Jun 3, 2008, at 2:07 PM, Kieran P wrote:

> Its passed from a text field on a form, so technically it would be  
> a string, but I guess it converts it on save because when I tried  
> to concat with another string, it wouldn’t change types.
> 
> And I’d really rather not change it to a string and back to int  
> just for one validation (and even if I did, I’m not sure how at  
> this point (before\_save ?)
> 
> Any other ideas?

this should work?

validates\_length\_of :year.to\_s, :is =\> 4

Or use validates\_format\_of with a regex that looks for 4 numerals. \d  
{4}

– gw

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 4, 2008, 2:00am UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/5 "2008-06-04T02:00:26Z")

</div>

Sorry Greg. Neither of those (:year.to\_s or format\_of) appear to work  
for  
some unknown reason.

Its very weird ☹

Regards  
Kieran

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 4, 2008, 2:08am UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/6 "2008-06-04T02:08:38Z")

</div>

Kieran P wrote:

> 1000 ← passes and it should  
> 10000 ← passes but shouldn’t
> 
> Has anyone got any ideas why this might be happening, or how to fix?
> 
> Using Rails 2.1.0. [http://2.1.0.](http://2.1.0.)

validates\_numericality\_of :year, :greater\_than =\> 999, :less\_than =\>  
10000

Lionel

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 4, 2008, 11:13am UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/7 "2008-06-04T11:13:37Z")

</div>

Thanks Lionel. That worked 😃 Finally my tests are passing 😃

Is the fact that length wouldn’t work on integers a bug in rails?

Regards  
Kieran

On Wed, Jun 4, 2008 at 6:41 PM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 4, 2008, 11:44am UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/8 "2008-06-04T11:44:36Z")

</div>

> Is the fact that length wouldn’t work on integers a bug in rails?

no, numbers have no length as such (I wouldn’t know of any programming  
language, library or whatever where they have)  
The actual length would at least depend on the used number system  
(decimal, hex…)

---

<div class="post-metadata">

**Author:** ![Kieran\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a4c791/32.png) [@Kieran\_P](https://www.ruby-forum.com/u/Kieran_P)\
**Post date:** [June 4, 2008, 8:41am UTC](https://www.ruby-forum.com/t/problem-with-validates-length-of/139392/9 "2008-06-04T08:41:35Z")

</div>

On 4 Jun 2008, at 00:59, Kieran P wrote:

> Sorry Greg. Neither of those (:year.to\_s or format\_of) appear to  
> work for some unknown reason.

Lionel gave the answer, but it’s worth pointing out that  
the :year.to\_s thing couldn’t possibly work, since it’s just the same  
as typing

validates\_length\_of ‘year’, :is =\> 4  
which we know doesn’t work.

Fred
