# Nil object nightmare

**URL:** <https://www.ruby-forum.com/t/nil-object-nightmare/174580>\
**Category:** Rails\
**Created:** [August 29, 2009, 1:19am UTC](https://www.ruby-forum.com/t/nil-object-nightmare/174580 "2009-08-29T01:19:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [August 29, 2009, 1:19am UTC](https://www.ruby-forum.com/t/nil-object-nightmare/174580/1 "2009-08-29T01:19:36Z")

</div>

I’m having a real problem with nil objects in a view I’m playing with.

if I type …

\<%= debug lesson.lesson\_register.attendees %\>

I get the following displayed in the browser …

- !ruby/object:Attendee  
attributes:  
enrollee\_id: “25”  
created\_at: 2009-08-27 15:10:41  
updated\_at: 2009-08-27 15:10:41  
id: “1”  
attended: f  
lesson\_register\_id: “1”  
attributes\_cache: {}

- !ruby/object:Attendee  
attributes:  
enrollee\_id: “26”  
created\_at: 2009-08-27 15:10:41  
updated\_at: 2009-08-27 15:10:41  
id: “2”  
attended: t  
lesson\_register\_id: “1”

if I type …

\<%= debug lesson.lesson\_register.attendees[0] %\>

I get correctly …

— !ruby/object:Attendee  
attributes:  
enrollee\_id: “25”  
created\_at: 2009-08-27 15:10:41  
updated\_at: 2009-08-27 15:10:41  
id: “1”  
attended: f  
lesson\_register\_id: “1”  
attributes\_cache: {}

BUT, when I type …

\<%= debug lesson.lesson\_register.attendees[0].created\_at %\>

I get …

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.created\_at

Extracted source (around line _#7_):

7: \<%= debug lesson.lesson\_register.attendees[0].created\_at %\>

Can someone tell me what I’m doing wrong.

TIA

-Ants

–  
100% naturally selected. 0% designed.
