# Displaying Nested Objects

**URL:** <https://discuss.rubyonrails.org/t/displaying-nested-objects/49929>\
**Category:** rubyonrails-talk\
**Created:** [March 7, 2010, 12:26am UTC](https://discuss.rubyonrails.org/t/displaying-nested-objects/49929 "2010-03-07T00:26:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ObjectEvolution](https://avatars.discourse-cdn.com/v4/letter/o/b3f665/32.png) [@ObjectEvolution](https://discuss.rubyonrails.org/u/ObjectEvolution)\
**Post date:** [March 7, 2010, 12:26am UTC](https://discuss.rubyonrails.org/t/displaying-nested-objects/49929/1 "2010-03-07T00:26:03Z")

</div>

Hi,

I've got a question regarding my models in my Rails app. Let's say I have an object:

User

which has\_many:

Thing1

and Thing1 has\_many:

Thing2

and Thing2 has\_one:

Thing3

When I go to display this in my template, like this:

\<table border="1"\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<th\>Title\</th\> &nbsp;&nbsp;&nbsp;&nbsp;\<th\>Date\</th\> &nbsp;&nbsp;&nbsp;&nbsp;\<th\>Event\</th\> &nbsp;&nbsp;\</tr\>

\<% @user.thing1.each do |c| %\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h c.title %\>\</td\> &nbsp;&nbsp;\<td\>\<%=h c.date %\>\</td\> &nbsp;&nbsp;\<td\> &nbsp;&nbsp;&nbsp;&nbsp;\<table\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<th\>Thing2\</th\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<% c.thing2.each do |be| %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%=h be.workout\_type %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%=h be.thing3.name %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\</table\> &nbsp;&nbsp;\</td\> &nbsp;&nbsp;\</tr\> \<% end %\>

I get:

undefined method `name' for nil:NilClass

I'm assuming this means there is no Thing3 object for this particular Thing2 object.

I just bought Agile Development with Rails and am starting to go through that...it's surely noob question but can someone set me straight?

Thanks!

Jon

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [March 7, 2010, 12:02pm UTC](https://discuss.rubyonrails.org/t/displaying-nested-objects/49929/2 "2010-03-07T12:02:18Z")

</div>

you're quite correct - there's no "thing3" linked to a given "thing 2". If there \*supposed\* to be an associated object there, then something's gone wrong when you've created your objects, but if it's a case that there may or may not be a Thing3, then a simple "guard" would suffice.

\<%=h be.thing3.name if be.thing3 %\>

---

<div class="post-metadata">

**Author:** ![ObjectEvolution](https://avatars.discourse-cdn.com/v4/letter/o/b3f665/32.png) [@ObjectEvolution](https://discuss.rubyonrails.org/u/ObjectEvolution)\
**Post date:** [March 7, 2010, 6:12pm UTC](https://discuss.rubyonrails.org/t/displaying-nested-objects/49929/3 "2010-03-07T18:12:13Z")

</div>

Perfect...thanks!
