# Nil object?

**URL:** <https://www.ruby-forum.com/t/nil-object/113316>\
**Category:** Rails\
**Created:** [August 29, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/nil-object/113316 "2007-08-29T19:31:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 29, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/nil-object/113316/1 "2007-08-29T19:31:29Z")

</div>

Hello i got this:

```
    <%Payment.find_all.each do |p|%>
 <li><a href="#"><%=p.member.name%>-<%=p.payer_id%></a></li>

```

\<%end%\>  
it throw :  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.name

but if i do the same in the script/console it just do it fine, but at  
the end of all it throw the same thing:

i got:

class Payment \< ActiveRecord::Base  
set\_table\_name :payments  
belongs\_to :member, :foreign\_key=\>:userid  
end

class Member \< ActiveRecord::Base  
set\_primary\_key :userid  
has\_many :payment, :foreign\_key=\>:userid  
end

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 29, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/nil-object/113316/2 "2007-08-29T20:06:17Z")

</div>

Oh sorry, but finally i got the mistake, is that in DB some records  
doesn’t match with the userid in member and this was throwing me this  
error, i just get in touch with this:

```
    <%Payment.find_all.each do |p|%>
  <li><a href="#"><%=p.member.name if 

```

p.member%\>-\<%#=p.payer\_id%\> Feb 9,  
2006  
\<%end%\>

Well now i can eat 😃

bye

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 29, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/nil-object/113316/3 "2007-08-29T20:30:38Z")

</div>

On 8/29/07, Edgar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello i got this:
> 
> ```
> <%Payment.find_all.each do |p|%>
> <li><a href="#"><%=p.member.name%>-<%=p.payer_id%></a></li>
> 
> ```
> 
> \<%end%\>  
> it throw :  
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.name

this means that it couldn’t find a member record for that particular  
instance of a payment object. So for that payment object, userid  
would be nil, which means you can’t dereference it to obtain a user.

A few comments about your code:

1. don’t use find\_all, it’s been deprecated. Use Payment.find(:all)  
instead
2. put your finders in your controller, that’s where they belong. Use  
@payments = Payment.find(:all) and then in your view do something like  
\<% @payments.each do |payment| %\>

> but if i do the same in the script/console it just do it fine, but at  
> the end of all it throw the same thing:
> 
> i got:
> 
> class Payment \< ActiveRecord::Base  
> set\_table\_name :payments  
> belongs\_to :member, :foreign\_key=\>:userid  
> end

you don’t need to use set\_table\_name here, the associated table name  
will by default be called ‘payments’

the rails convention for foreign key naming is to use classname\_id.  
So you should get into the habit of using user\_id to reference a User  
object. In this case it doesn’t really matter, but you might as well  
follow convention.

Adam

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 29, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/nil-object/113316/4 "2007-08-29T21:55:31Z")

</div>

Hey thanks a lot Adam, very constructive answer, relly thanks,

good luck
