# Handling of nil objects

**URL:** <https://www.ruby-forum.com/t/handling-of-nil-objects/72106>\
**Category:** Rails\
**Created:** [October 14, 2006, 6:22am UTC](https://www.ruby-forum.com/t/handling-of-nil-objects/72106 "2006-10-14T06:22:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [October 14, 2006, 6:22am UTC](https://www.ruby-forum.com/t/handling-of-nil-objects/72106/1 "2006-10-14T06:22:30Z")

</div>

to paint the picture

I currently have a system which lists uses and the Department the user  
is assigned to. So my models look something like this.

####User Model  
class User \< ActiveRecord::Base  
belongs\_to :department  
end

####Department Model  
class Department \< ActiveRecord::Base  
belongs\_to :organization  
has\_many :users  
end

A user may not necessarily have a department which is fine however, when  
this user does not have a department this department value will be nil.  
When I go to print out this department value I get the following error

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.name

the name value is my department name. What’s the best way to tell rails  
to print out nothing if this object is set to nil?

I was thinking of setting up a helper method. But I’m not sure that  
that is the best thing to do.

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [October 14, 2006, 9:59am UTC](https://www.ruby-forum.com/t/handling-of-nil-objects/72106/2 "2006-10-14T09:59:27Z")

</div>

Using a method in your User model in place of accessing the  
relationship’s attributes is probably the best way to handle this.

####User Model  
class User \< ActiveRecord::Base  
belongs\_to :department

def department\_name  
department.nil? ? ‘’ : department.name  
end  
end

Whenever you need the department name use that method  
user.department\_name. Extending this a bit, you could open it up for  
all fields (renamed to prevent a conflict with the real attr\_accessor  
for department):

def department\_attribute(field)  
department.nil? ? ‘’ : department.send(field)  
end

Then you can do user.department\_attribute(:name).

Or, you could turn off whiny nils (see  
config/environments/development.rb), at the expense of obscuring bugs.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [October 14, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/handling-of-nil-objects/72106/3 "2006-10-14T16:08:27Z")

</div>

On Oct 14, 9:20 am, Stewart [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> Posted viahttp://www.ruby-forum.com/.  
> You could also do stuff like this…

@department.department\_name rescue “None”

or override the reader for the attribute…

class User  
def department\_name  
self[:department\_name] || “None”  
end  
end

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [October 14, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/handling-of-nil-objects/72106/4 "2006-10-14T15:20:07Z")

</div>

Eden Li wrote:

> Using a method in your User model in place of accessing the  
> relationship’s attributes is probably the best way to handle this.
> 
> ####User Model  
> class User \< ActiveRecord::Base  
> belongs\_to :department
> 
> def department\_name  
> department.nil? ? ‘’ : department.name  
> end  
> end
> 
> Whenever you need the department name use that method  
> user.department\_name. Extending this a bit, you could open it up for  
> all fields (renamed to prevent a conflict with the real attr\_accessor  
> for department):
> 
> def department\_attribute(field)  
> department.nil? ? ‘’ : department.send(field)  
> end
> 
> Then you can do user.department\_attribute(:name).
> 
> Or, you could turn off whiny nils (see  
> config/environments/development.rb), at the expense of obscuring bugs.

That’s perfect! I ended up defining a method in the user model. This  
will help me with a number of other issues I was facing. I did not  
fully understand implementing methods in our model before. I was just  
used to declaring them for my controllers. Thank you so much for your  
help!
