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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 9 Jan 2008, at 15:38, Luca Roma 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> Why? > > How i can do? > Thanks > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---