# Nil objects

**URL:** <https://www.ruby-forum.com/t/nil-objects/84640>\
**Category:** Ruby-Forum\
**Created:** [February 6, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/nil-objects/84640 "2007-02-06T17:32:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Gregianin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@Alex\_Gregianin](https://www.ruby-forum.com/u/Alex_Gregianin)\
**Post date:** [February 6, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/nil-objects/84640/1 "2007-02-06T17:32:39Z")

</div>

Hello ruby People!

I’m learning ruby right now,and i got a easy question for you guys!  
whenever i have a model with nil objects,when i try to actually see the  
record I get the error

“You have a nil object when you didn’t expect it!”

I knwo what this error means and i know why it’s happenning in my  
case,but my question is:

```
  what should i do to make rails show nil objects as blank

```

fields?like I’m try ing to add a new user to my system and there’s 4  
fields: id,username,password,description…

let’s say i did fill 2 of them(id is auto increment),and left the last  
one blank…now i wanna show my record into my view,but I’m sure i will  
get this error…

You have a nil object when you didn’t expect it!

```
 i know how to work around this also...i did something like this :

```

unless @model.name.nil?  
@model.name  
end

but if have to do this for EVERY field into my view,i pretty much feel  
like rails isn’t being as much productivity as it’s supposed to be…

i’m sorry for my english and of course if i’m saying something stupid  
please correct me guys! thankz man!

---

<div class="post-metadata">

**Author:** ![Alex\_Gregianin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@Alex\_Gregianin](https://www.ruby-forum.com/u/Alex_Gregianin)\
**Post date:** [February 25, 2007, 11:44am UTC](https://www.ruby-forum.com/t/nil-objects/84640/2 "2007-02-25T11:44:27Z")

</div>

Alex Gregianin wrote:

> Hello ruby People!
> 
> I’m learning ruby right now,and i got a easy question for you guys!  
> whenever i have a model with nil objects,when i try to actually see the  
> record I get the error
> 
> “You have a nil object when you didn’t expect it!”

I was searching for this exact same thing, too bad no one had responded  
to you yet.

One solution would be to extend the NilClass to handle missing methods  
so it would behave  
like /dev/null, always returning nil instead of some runtime error.  
Example nil.nonexistentmethod =\> nil

I’d like to know what others think about this.

---

<div class="post-metadata">

**Author:** ![Alex\_Gregianin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@Alex\_Gregianin](https://www.ruby-forum.com/u/Alex_Gregianin)\
**Post date:** [September 17, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/nil-objects/84640/3 "2007-09-17T19:33:44Z")

</div>

Anonymous wrote:

> Alex Gregianin wrote:
> 
> I’d like to know what others think about this.

For what it’s worth at this point:

\<% @users.each do |u| %\>  
\<%= u.name rescue nil %\>  
\<% end %\>

will produce an empty space in your view where the data would go if  
there is no value in that record. So unless you don’t want a record to  
show up at all is missing fields, you don’t need the extra helper  
methods…

Cheers,  
divotdave

---

<div class="post-metadata">

**Author:** ![Alex\_Gregianin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@Alex\_Gregianin](https://www.ruby-forum.com/u/Alex_Gregianin)\
**Post date:** [October 4, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/nil-objects/84640/4 "2007-10-04T14:10:34Z")

</div>

David H. wrote:  
…

> \<%= u.name rescue nil %\>  
> …  
> Neat trick - thanks very much!
