# Nil object when an array was expected

**URL:** <https://www.ruby-forum.com/t/nil-object-when-an-array-was-expected/83105>\
**Category:** Rails\
**Created:** [January 23, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/nil-object-when-an-array-was-expected/83105 "2007-01-23T18:43:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chhenning](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chhenning](https://www.ruby-forum.com/u/chhenning)\
**Post date:** [January 23, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/nil-object-when-an-array-was-expected/83105/1 "2007-01-23T18:43:33Z")

</div>

Hi there, it’s kind of a weird problem. I have a Person model and  
Person controller. In the new method in my controller I try to retrieve  
a collection of nationalities. The nationalities are stored in a  
different table. So retrieving those should be like:

def new  
@person = Person.new  
@person.name = Name.new  
@person.birthday = Date.today  
@person.nationality = Nationality.new  
nationalities = Nationality.find( :all )  
end

Unfortunately when Rails try to access the array in the new.rhtml it’s  
nil. Here, I create a selection list, as follows:

\<% form\_for :gender do |form| %\> \<%= form.select( :gender, @genders ) %\> \<% end %\>

The weird thing happens when I retrieve the array inside new.rhtml.  
It’s working here.

\<% form\_for :nationality do |form| %\> \<%= @nationalities = Nationality.find( :all ) form.select( :nationality, @nationalities ) %\> \<% end %\>

As stated in the new Web D. book those code should normally  
reside in the controller.

Am I missing something?

Thanks ahead,  
Christian

---

<div class="post-metadata">

**Author:** ![chhenning](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chhenning](https://www.ruby-forum.com/u/chhenning)\
**Post date:** [January 23, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/nil-object-when-an-array-was-expected/83105/2 "2007-01-23T18:57:36Z")

</div>

Try using an instance variable (@nationalities) in the controller  
instead of a local variable (nationalities). Instance variables are  
available in the views, but local variables created in the controller  
method are not.

Jason

On Tue, 23 Jan 2007 17:29:04 -0000  
“chhenning” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @person.nationality = Nationality.new
> 
> ```
> 
> %\>
> 
> >

–  
Jason S. | Tel: 616-532-2300  
Systems Administrator/ | Fax: 616-532-3461  
Programmer | Email: removed\_email\_address@domain.invalid  
Right to Life of Michigan | Web: [http://www.rtl.org](http://www.rtl.org)

---

<div class="post-metadata">

**Author:** ![chhenning](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chhenning](https://www.ruby-forum.com/u/chhenning)\
**Post date:** [January 23, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/nil-object-when-an-array-was-expected/83105/3 "2007-01-23T20:04:52Z")

</div>

Thanks Jason, I’m coming from C++ and though I’m probably more used to  
C++ way of compiler errors. If an variables is not accessible in the  
current scope why not just state that. But I guess Ruby is on a lot of  
ways different to C++.

Anyway thanks, that was the solution.

Christian
