# Why won't this array populate?

**URL:** <https://www.ruby-forum.com/t/why-wont-this-array-populate/75518>\
**Category:** Rails\
**Created:** [November 10, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/why-wont-this-array-populate/75518 "2006-11-10T16:38:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 10, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/why-wont-this-array-populate/75518/1 "2006-11-10T16:38:43Z")

</div>

Units have many rooms. Rooms belong to units. When I show the detail  
of a unit, I want to show a list of rooms belonging to the unit as well.  
In the UnitController:

def show  
@unit = Unit.find(params[:id])  
session[:unit\_id] = @unit.id  
@room = Room.find(:all,  
:conditions =\> [“unit\_id = ?”, @unit[:id] ])  
end

In /units/show.rhtml:

# Room Info
\<% unless @room.nil? %\> 
 \<% for room in @rooms %\> - \<%= @room.name %\> \<% end %\> 

\<% end %\>

Yet I still get this exception:

NoMethodError in Units#show

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.each

Questions:

1. Why do I have a nil array?
2. Even if I have a nil array, why doesn’t \<% unless @room.nil? %\> stop  
it?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 10, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/why-wont-this-array-populate/75518/2 "2006-11-10T16:53:05Z")

</div>

Because in show you are using @rooms which you haven’t set anywhere

Fred

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 10, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/why-wont-this-array-populate/75518/3 "2006-11-10T16:58:46Z")

</div>

Taylor S. wrote:

> \<% end %\>
> 
> Questions:
> 
> 1. Why do I have a nil array?
> 2. Even if I have a nil array, why doesn’t \<% unless @room.nil? %\> stop  
> it?

Hey

Nowhere in your action is @rooms defined…

I think it should look as follows:

def show  
@unit = Unit.find(params[:id])  
session[:unit\_id] = @unit.id  
@rooms = @unit.rooms #assuming as you say unit “has\_many :rooms”  
end

And in the view do the following:

 \<% for room in @rooms %\> - \<%=h room.name %\>
 \<% end %\>

I hope that helps man!

Cheery-o  
Gustav P.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 10, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/why-wont-this-array-populate/75518/4 "2006-11-10T18:59:04Z")

</div>

Gustav P. \<gustav@…\> writes:

> ```
> <li><%=h room.name %></li>
> 
> ```
> 
> \<% end %\>

No need to even set an instance variable

 \<% for room in @unit.rooms %\> - \<%=h room.name %\>
 \<% end %\> 

Gareth

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 10, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/why-wont-this-array-populate/75518/5 "2006-11-10T17:27:21Z")

</div>

Fred:  
You are right. A simple typo caused most of it.

Gustav:  
Your way is much neater! Why bust my fingers when ActiveRecord can do  
it for me? Thanks!
