All, I know this question has been asked before, but gmane is down: How do I access the object that is defined by a ''belongs_to'' relationship? Simple example: :Book belongs_to :author :Author has_one :book The foreign id of the Author is present in the book table. For a certain instance of a book, I want to access the authors name - couldn''t I use book.author.name? I get a nil object when I try this. -- - Nic --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 23 Aug 2006, at 00:37, Nic Werner wrote:> > All, > I know this question has been asked before, but gmane is down: > > How do I access the object that is defined by a ''belongs_to'' > relationship? > > Simple example: > > :Book belongs_to :author > :Author has_one :book > > The foreign id of the Author is present in the book table. > > For a certain instance of a book, I want to access the authors name - > couldn''t I use book.author.name? > > I get a nil object when I try this.Hi Nic, Ensure your foreign key is named correctly. You''d want author_id in your book table. Make sure this is correct and then it should be fine. Alastair ------ Alastair Moore Standards compliant web development with Ruby On Rails, PHP and ASP www.kozmo.co.uk 07738 399038 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
>> All, >> I know this question has been asked before, but gmane is down: >> >> How do I access the object that is defined by a ''belongs_to'' >> relationship? >> >> Simple example: >> >> :Book belongs_to :author >> :Author has_one :book >> >> The foreign id of the Author is present in the book table. >> >> For a certain instance of a book, I want to access the authors name - >> couldn''t I use book.author.name? >> >> I get a nil object when I try this. > > Hi Nic, > > Ensure your foreign key is named correctly. You''d want author_id in > your book table. Make sure this is correct and then it should be fine. >And make sure there''s a legitimate entry in the author table for that book... :) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---