# Common has\_one accessing question

**URL:** <https://discuss.rubyonrails.org/t/common-has-one-accessing-question/286>\
**Category:** rubyonrails-talk\
**Created:** [August 22, 2006, 11:37pm UTC](https://discuss.rubyonrails.org/t/common-has-one-accessing-question/286 "2006-08-22T23:37:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nic\_Werner](https://avatars.discourse-cdn.com/v4/letter/n/d2c977/32.png) [@Nic\_Werner](https://discuss.rubyonrails.org/u/Nic_Werner)\
**Post date:** [August 22, 2006, 11:37pm UTC](https://discuss.rubyonrails.org/t/common-has-one-accessing-question/286/1 "2006-08-22T23:37:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alastair](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Alastair](https://discuss.rubyonrails.org/u/Alastair)\
**Post date:** [August 22, 2006, 11:43pm UTC](https://discuss.rubyonrails.org/t/common-has-one-accessing-question/286/2 "2006-08-22T23:43:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom1](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Philip\_Hallstrom1](https://discuss.rubyonrails.org/u/Philip_Hallstrom1)\
**Post date:** [August 23, 2006, 12:20am UTC](https://discuss.rubyonrails.org/t/common-has-one-accessing-question/286/3 "2006-08-23T00:20:32Z")

</div>

> > 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... 🙂
