# Avoiding nil object error?

**URL:** <https://www.ruby-forum.com/t/avoiding-nil-object-error/76154>\
**Category:** Rails\
**Created:** [March 3, 2006, 7:35am UTC](https://www.ruby-forum.com/t/avoiding-nil-object-error/76154 "2006-03-03T07:35:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 3, 2006, 7:35am UTC](https://www.ruby-forum.com/t/avoiding-nil-object-error/76154/1 "2006-03-03T07:35:21Z")

</div>

I’m a total Rails newbie and i’ve been struggling for hours today  
with one (prolly very silly) problem:

I have a table portfolios that has many images:

class Portfolio \< ActiveRecord::Base  
has\_many :images  
end

class Image \< ActiveRecord::Base  
belongs\_to :portfolios  
end

In the controller i define a list of active portfolios:

@active\_portfolios = Portfolio.find\_all\_by\_is\_active(“1”)

In the view i render the list of portfolios:  
\<%= render :partial =\> “portfolio\_list”, :collection =\>  
@active\_portfolios %\>

in the partial collection \_portfolio\_list i need to get the  
image\_url of the first image of the portfolio:

 ![]()

This works fine if every portfolio has at least one image assigned to  
it. However, if a portfolio doesn’t have any images yet, the whole  
thing falls apart and the app exits with the error:

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

I tried to get around it by doing this in the portfolio model:

def get\_first\_image  
self.images[0].image\_url || “default.jpg”  
end

and then in the partial:

 ![]()

but the exact same thing happens. If every portfolio has at least one  
image it works well, if not, the error comes up. How can i avoid  
errors on nil objects and instead define alternatives (e.g.  
default.jpg) if a child attribute does not exist ?

Thanks for your help.  
Sebastian

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 3, 2006, 7:44am UTC](https://www.ruby-forum.com/t/avoiding-nil-object-error/76154/2 "2006-03-03T07:44:24Z")

</div>

> @active\_portfolios = Portfolio.find\_all\_by\_is\_active(“1”)
> 
> In the view i render the list of portfolios:  
> \<%= render :partial =\> “portfolio\_list”, :collection =\> @active\_portfolios  
> %\>

\<% unless portfolio.images[0].nil? -%\>

 ![]()\<% end -%\>

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 3, 2006, 7:56am UTC](https://www.ruby-forum.com/t/avoiding-nil-object-error/76154/3 "2006-03-03T07:56:32Z")

</div>

That’s the ticket. 🙂 Thank you so much.  
sebastian

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 3, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/avoiding-nil-object-error/76154/4 "2006-03-03T16:09:15Z")

</div>

you were very close with your get\_first\_image method

class Portfolio \<\< ActiveRecord::Base  
…

# define a default image url

DEFAULT\_IMG = “default.jpg”

# return the first image url in the portfolio or a default image if no

images exist  
def first\_image  
(images.empty?) ? DEFAULT\_IMG : images[0].image\_url  
end  
end

then in your view:

![]()

this has the benefit of removing the logic from the view and into the  
portfolio where it belongs.

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 3, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/avoiding-nil-object-error/76154/5 "2006-03-03T19:35:25Z")

</div>

> end
> 
> then in your view:
> 
> ![]()
> 
> this has the benefit of removing the logic from the view and into the  
> portfolio where it belongs.

good suggestion! for those of us without a C/C++ background:

def first\_image  
if images.empty? then DEFAULT\_IMG else image[0].image\_url end  
end

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 3, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/avoiding-nil-object-error/76154/6 "2006-03-03T18:37:53Z")

</div>

I had ended up doing this in similar (but much uglier) way all in the  
view:

\<img src="\<%= portfolio\_list.images[0].nil? ? “default.jpg” :  
portfolio\_list.images[0]. image\_url -%\>" /\>

but i certainly like your suggestion much better, especially when it  
comes to quickly changing the defaults etc. I’m still trying to get  
into the mindset of MVC separation and these kind of suggestions help  
very much. Thanks.

Sebastian
