Relationship not working

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.

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

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.