Common has_one accessing question

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

On 23 Aug 2006, at 00:37, Nic W. wrote:

: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 M.
Standards compliant web development with Ruby On Rails, PHP and ASP
www.kozmo.co.uk
07738 399038

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… :slight_smile: