Forum: Ruby on Rails validates_length_of INCONSISTENT BEHAVIOR

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.
Kad K. (Guest)
on 2007-06-23 21:50
In one app (Beast app example) the User class has a login validation :
  validates_length_of       :login, :minimum => 2
the users table  has been created .. with one record   login 'kadoudal'

  create_table "users", :force => true do |t|
    t.column "login",                :string
    t.column "email",                :string

I check with script/console =>  >> User.find(:first)
and I get the user
#<User:0x34e5e54 @attributes={"login"=>"kadoudal" .......
-----
In my app
I  replicated the User class  ( copied exactly same code)
I copied the 'users' table into my DB and the schema.db is exactly the
same...

I check with script/console =>  >> User.find(:first)
and I got an error !!

NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.%
        from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/validations.rb:487:in
`validates_size_of'
        from
/usr/local/lib/ruby/gems/1.8/gems/gettext-1.9.0/lib/gettext/active_record.rb:85:in
`validates_length_of'
        from ./script/../config/../config/../app/models/user.rb:6
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:203:in
`load_file'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in
`new_constants_in'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:202:in
`load_file'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:94:in
`require_or_load'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:248:in
`load_missing_constant'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:452:in
`const_missing'
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:464:in
`const_missing'
        from (irb):1

and the line 6 in models/user.rb is  ...   validates_length_of
:login, :minimum => 2

where could be the origin of such different behavior ?  (...)

thanks for your suggestions

kad
Chris K. (Guest)
on 2007-06-24 04:40
Kad K. wrote:
> NoMethodError: You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.%
>         from
> 
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/validations.rb:487:in
> `validates_size_of'
>         from
> /usr/local/lib/ruby/gems/1.8/gems/gettext-1.9.0/lib/gettext/active_record.rb:85:in
> `validates_length_of'
>         from ./script/../config/../config/../app/models/user.rb:6
>         from

A guess: it has something to do with the validation message. Go check
out active_record/validations.rb:487, where your error is occurring.
You'll see that the error is from calling % on an apparently nil string.
Are you sure you're not passing an invalid :message or :too_short in the
options hash? Or perhaps, as the next level of the stack suggests,
GetText is messing around with it?

Try disabling GetText temporarily. If you can't figure it out, post more
of your model so we can see what's going on.

Chris K.
http://kampers.net
Kad K. (Guest)
on 2007-06-24 09:38
Chris K. wrote:
> Kad K. wrote:
>> NoMethodError: You have a nil object when you didn't expect it!
>> The error occurred while evaluating nil.%
>>         from
>> 
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/validations.rb:487:in
>> `validates_size_of'
>>         from
>> /usr/local/lib/ruby/gems/1.8/gems/gettext-1.9.0/lib/gettext/active_record.rb:85:in
>> `validates_length_of'
>>         from ./script/../config/../config/../app/models/user.rb:6
>>         from
>
> A guess: it has something to do with the validation message. Go check
> out active_record/validations.rb:487, where your error is occurring.
> You'll see that the error is from calling % on an apparently nil string.
> Are you sure you're not passing an invalid :message or :too_short in the
> options hash? Or perhaps, as the next level of the stack suggests,
> GetText is messing around with it?
>
> Try disabling GetText temporarily. If you can't figure it out, post more
> of your model so we can see what's going on.
>
> Chris K.
> http://kampers.net

Thanks Chris
Very strange...

I modified the config/environments/development.rb  (as per the beast
example, mine was different...)

=>   config.cache_classes = true

and it runs ok ...  now figuring why is another step .....
This topic is locked and can not be replied to.