Problem with association

i have 2 models:

class User < ActiveRecord::Base
has_many :mails
end

class Mail < ActiveRecord::Base
belongs_to :receiver ,:class_name => ‘User’,:foreign_key =>
‘receiver_id’
belongs_to :sender,:class_name => ‘User’,:foreign_key => ‘sender_id’
end

in controller i have this:
@messages=Mail.find(:all,:include=>:sender,:conditions=>“receiver_id=#{session[:user_id]}”,:limit=>40,:offset=>(params[‘page’]
? params[‘page’] : 0))

if i put @messages[0].sender.name

i receive an error:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name

Why?

How i can do?
Thanks

On 9 Jan 2008, at 15:38, Luca R. wrote:

if i put @messages[0].sender.name

i receive an error:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name

Well the obvious reason is that you have a message with a nil
message.sender. Is this the case ?

Fred

Here’s a hint: Your User model has_many :mails, but a user really
has_many :sent_mails and has_many :received_mails. You’ll need to set
the foreign_key attributes.