Nil object?

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

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 :smiley:

bye

On 8/29/07, Edgar G. [email protected] 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

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

good luck