# Rails 2.1 and validates\_length\_of

**URL:** <https://www.ruby-forum.com/t/rails-2-1-and-validates-length-of/139785>\
**Category:** Rails\
**Created:** [June 6, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/rails-2-1-and-validates-length-of/139785 "2008-06-06T23:43:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [June 6, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/rails-2-1-and-validates-length-of/139785/1 "2008-06-06T23:43:38Z")

</div>

Hi,

I’m getting some weirdness when using validates\_length\_of and Rails  
2.1

class User \< ActiveRecord::Base  
include Authentication  
include Authentication::ByPassword  
include Authentication::ByCookieToken

validates\_presence\_of :login  
validates\_length\_of :login, :within =\> 3…40  
validates\_uniqueness\_of :login, :case\_sensitive =\> false  
validates\_format\_of :login, :with =\> RE\_LOGIN\_OK, :message  
=\> MSG\_LOGIN\_BAD

validates\_format\_of :first\_name, :last\_name, :with =\>  
RE\_NAME\_OK, :message =\> MSG\_NAME\_BAD, :allow\_nil =\> true  
validates\_length\_of :first\_name, :maximum =\> 50, :allow\_nil  
=\> true  
validates\_length\_of :last\_name, :maximum =\> 50

validates\_presence\_of :email  
validates\_length\_of :email, :within =\> 6…100 #removed\_email\_address@domain.invalid  
validates\_uniqueness\_of :email, :case\_sensitive =\> false  
validates\_format\_of :email, :with =\> RE\_EMAIL\_OK, :message  
=\> MSG\_EMAIL\_BAD

belongs\_to :company  
validates\_existence\_of :company

Pretty straightforward so far.

I jump into script/console, and things get weird.

> > record = User.new({ :login =\> ‘quire’, :email =\> ‘removed\_email\_address@domain.invalid’, :password =\> ‘quire69’, :password\_confirmation =\> ‘quire69’ })  
> > =\> #\<User id: nil, login: “quire”, first\_name: nil, last\_name: nil,  
> > email: “removed\_email\_address@domain.invalid”, crypted\_password: nil, salt: nil,  
> > created\_at: nil, updated\_at: nil, remember\_token: nil,  
> > remember\_token\_expires\_at: nil, company\_id: nil\>
> 
> > record.valid?  
> > =\> false
> 
> > record.errors.full\_messages  
> > =\> [“Company does not exist”, “First name is too long (maximum is 50  
> > characters)”, “Last name is too long (maximum is 50 characters)”]
> 
> > record.first\_name  
> > =\> nil

By my reading of the documentation and the source, the :maximum  
and :minimum methods shouldn’t flag, especially with :allow\_nil set to  
true on the first\_name field.

This just seems to basic to miss, so I want to make sure I’m not  
completely crazy before submitting a bug.

(OS X, Ruby 1.8.6 p110, Rails 2.1)

Thanks,

Andrew
