# Strange array behaviour

**URL:** <https://www.ruby-forum.com/t/strange-array-behaviour/180702>\
**Category:** Rails\
**Created:** [December 26, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/180702 "2009-12-26T21:22:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 26, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/180702/1 "2009-12-26T21:22:33Z")

</div>

I’ve got a relationship:

Person has many MiddleNames  
and  
MiddleName belongs to Person

In irb I can add a person, add a middle name then call

@person.middle\_names.first.name

to get that middle name. However when I try the exact same code in a  
view, I  
get

undefined method `name’ for nil:NilClass

If I iterate through like so:

for mname in @person.middle\_names  
puts mname.name

Then it works perfectly, but for some reason using “.first” is failing  
in  
the view. I’ve used this method many many times and never come across  
anything quite like this.

I believe I’m probably overlooking some incredibly simple answer and I’m  
going to kick myself when someone points it out, but for now I’m  
completely  
stumpted.

Any input appreciated.

Thanks

Matt

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 26, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/180702/2 "2009-12-26T23:40:00Z")

</div>

> for mname in @person.middle\_names  
> puts mname.name
> 
> Then it works perfectly, but for some reason using “.first” is failing  
> in  
> the view. I’ve used this method many many times and never come across  
> anything quite like this.

“.first” is an array method. “mname.name” is an instance of an array…

Hope this helps…

> I believe I’m probably overlooking some incredibly simple answer and I’m  
> going to kick myself when someone points it out, but for now I’m  
> completely stumped.

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 26, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/180702/3 "2009-12-26T23:45:20Z")

</div>

On Sat, Dec 26, 2009 at 11:40:00PM +0100, Pale H. wrote:

> Hope this helps…  
> Thanks, but I’ve just noticed something:

The error I am getting only appears from my testing with  
cucumber/rspec/webrat. If I  
access the page manually through passenger, it all works perfectly.

I might contact the cuke list about this as the code is obviously  
correct, it works  
fine in irb and in a real browser, just not in the test environment. So  
much for me  
sticking to my BDD plans 😃

Thanks

Matt

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 27, 2009, 6:56am UTC](https://www.ruby-forum.com/t/strange-array-behaviour/180702/4 "2009-12-27T06:56:06Z")

</div>

On Sat, Dec 26, 2009 at 09:43:26PM -0500, Rick DeNatale wrote:

> > @person.middle\_names.first.name
> 
> And what happens if you don’t add a middle name.
> 
> [].first #=\> nil

That is true, but in my scenario I have added a middle name and related  
it to that  
person. I will of course go and triple check that but I have executed  
the copy and  
pasted commands from the cuke steps and it worked in irb.

Its quite possible I have slipped up but I will go and check. Thanks

> > ?puts mname.name
> 
> Because if @person.middle\_names is empty the loop body is not executed.

But it does execute the loop, and prints out the first of the names as  
expected. This  
is the weird thing.

> > Then it works perfectly, but for some reason using “.first” is failing in  
> > the view. I’ve used this method many many times and never come across  
> > anything quite like this.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [December 27, 2009, 3:44am UTC](https://www.ruby-forum.com/t/strange-array-behaviour/180702/5 "2009-12-27T03:44:42Z")

</div>

On Sat, Dec 26, 2009 at 3:20 PM, Matt H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve got a relationship:
> 
> Person has many MiddleNames  
> and  
> MiddleName belongs to Person
> 
> In irb I can add a person, add a middle name then call
> 
> @person.middle\_names.first.name

And what happens if you don’t add a middle name.

[].first #=\> nil

> to get that middle name. However when I try the exact same code in a view, I  
> get
> 
> undefined method `name’ for nil:NilClass
> 
> If I iterate through like so:
> 
> for mname in @person.middle\_names  
> puts mname.name

Because if @person.middle\_names is empty the loop body is not executed.

> Thanks
> 
> Matt

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [January 1, 2010, 12:38pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/180702/6 "2010-01-01T12:38:40Z")

</div>

maybe ‘puts @person.middle\_names.inspect’ will give some information of  
the structure .

On 2009å¹´12æœˆ27æ—¥ 05:54, Matt H. wrote:

> > >
> 
> person. I will of course go and triple check that but I have executed the copy and
> 
> > >
> 
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
