# Relationship not working

**URL:** <https://www.ruby-forum.com/t/relationship-not-working/63963>\
**Category:** Rails\
**Created:** [January 22, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/relationship-not-working/63963 "2006-01-22T17:18:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [January 22, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/relationship-not-working/63963/1 "2006-01-22T17:18:27Z")

</div>

Hi all

I’m in the process of trying to make my first project on Rails after  
doing the tutorial in AWDR. I picked a project which I thought would be  
easy as I already have the PHP/MySQL implementation. Unfortunately I’ve  
now hit a snag and was wondering if you could help?

I have a table called “races” and a table called “courses”. The  
relationship is: course -\< races. Course can have many races.

In the Course model I have “has\_many :races” and in the Race model I  
have: “belongs\_to :course”.

So far so good?

Well when I list the races I want a column that shows the course it  
belongs to as well as the data in the race table. Having looked at the  
Rolling with Rails tutorial for help it should simply be a case of  
refrencing it like: “race.course.name”.

However I’m getting an error that appears as though the relationship  
doesn’t exist as it displays:  
“You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.name”

I’d be very grateful for your help.

I have the feeling most of my problems so far are due to my rusty OO and  
MVC practice which hasn’t been helped by being a ‘spaghetti’ PHP coder  
for so long.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [January 22, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/relationship-not-working/63963/2 "2006-01-22T17:25:13Z")

</div>

Do all races have a course\_id?? If a race doesn’t have a associated  
course, you will probably get a nil (NULL in PHP).

Try setting a course\_id and try again.

If that doesn’t work, could you post more code?

Jules

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [January 22, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/relationship-not-working/63963/3 "2006-01-22T17:28:03Z")

</div>

Thank you Jules. Very much appreciated. You were absolutely right - I  
had a null value for the course\_id in the race record.

Its sorted now.
