# Nil object when an array was expected.

**URL:** <https://discuss.rubyonrails.org/t/nil-object-when-an-array-was-expected/7661>\
**Category:** rubyonrails-talk\
**Created:** [January 23, 2007, 5:29pm UTC](https://discuss.rubyonrails.org/t/nil-object-when-an-array-was-expected/7661 "2007-01-23T17:29:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chhenning](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@chhenning](https://discuss.rubyonrails.org/u/chhenning)\
**Post date:** [January 23, 2007, 5:29pm UTC](https://discuss.rubyonrails.org/t/nil-object-when-an-array-was-expected/7661/1 "2007-01-23T17:29:04Z")

</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 &nbsp;&nbsp;&nbsp;&nbsp;@person = Person.new &nbsp;&nbsp;&nbsp;&nbsp;@person.name = Name.new &nbsp;&nbsp;&nbsp;&nbsp;@person.birthday = Date.today &nbsp;&nbsp;&nbsp;&nbsp;@person.nationality = Nationality.new &nbsp;&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<% form\_for :gender do |form| %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= form.select( :gender, @genders ) %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;\</p\>

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

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

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

Am I missing something?

Thanks ahead, Christian

---

<div class="post-metadata">

**Author:** ![Jason\_Stewart1](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Jason\_Stewart1](https://discuss.rubyonrails.org/u/Jason_Stewart1)\
**Post date:** [January 23, 2007, 5:57pm UTC](https://discuss.rubyonrails.org/t/nil-object-when-an-array-was-expected/7661/2 "2007-01-23T17:57:37Z")

</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

---

<div class="post-metadata">

**Author:** ![chhenning](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@chhenning](https://discuss.rubyonrails.org/u/chhenning)\
**Post date:** [January 23, 2007, 6:55pm UTC](https://discuss.rubyonrails.org/t/nil-object-when-an-array-was-expected/7661/3 "2007-01-23T18:55:30Z")

</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
