Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Called id for nil in Rails 3

In development mode:

nil.id
=> "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"

In production mode:

nil.id
=> 4

Why?

like image 873
Shaliko Usubov Avatar asked Sep 26 '11 12:09

Shaliko Usubov


2 Answers

Look for the line that says the following in your environments configs:

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true # or false in production.rb

This is to prevent you from calling methods on nil while in development mode. I guess they disabled it for performance reasons in production.

And nil is a singleton object in ruby, that's why its id will be 4 no matter what.

like image 75
Benoit Garret Avatar answered Oct 05 '22 07:10

Benoit Garret


Your development.rb evironment has the following line:

 config.whiny_nils = true

Which will log an error when you try to call a method on nil. nil's id is 4 because it is an object which happens to have an id of 4

like image 43
Dan McClain Avatar answered Oct 05 '22 08:10

Dan McClain