# Associations problem

**URL:** <https://www.ruby-forum.com/t/associations-problem/172390>\
**Category:** Rails\
**Created:** [July 20, 2009, 11:55pm UTC](https://www.ruby-forum.com/t/associations-problem/172390 "2009-07-20T23:55:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robin\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@Robin\_F](https://www.ruby-forum.com/u/Robin_F)\
**Post date:** [July 20, 2009, 11:55pm UTC](https://www.ruby-forum.com/t/associations-problem/172390/1 "2009-07-20T23:55:49Z")

</div>

Hi all,

I have some associations setup such that:

class Job  
has\_many :applications  
end

class Candidate  
has\_many :applications  
end

class Application  
belongs\_to :job  
belongs\_to :candidate  
end

When viewing a particular job, I have the following iteration:

\<% for application in @job.applications %\>  
\<%= h application.candidate.name %\>  
\<% end %\>

I am getting a nil object error when evaluating nil.name. If I delete  
“name”, I can see the candidate object and if I change it  
application.candidate\_id, I see the candidate\_id for the application.  
I have setting the @applications variable in the controller rather  
than go through the association in the view but still get the same  
problem.

My only thought is that maybe the model named Application is causing  
trouble but wouldn’t want to change this on a hunch!

Does anyone have any thoughts?

Thanks

Robin

---

<div class="post-metadata">

**Author:** ![Robin\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@Robin\_F](https://www.ruby-forum.com/u/Robin_F)\
**Post date:** [July 21, 2009, 1:04am UTC](https://www.ruby-forum.com/t/associations-problem/172390/2 "2009-07-21T01:04:16Z")

</div>

I don’t know if application is reserved, I would guess it is not since  
models don’t use this term, but try what I have below before renaming  
things.

In my experience you need to check the candidate is not nil.  
Especially since you aren’t requiring an application has a candidate.

\<% for application in @job.applications %\>  
\<% if application.candidate != nil %\>  
\<%= h application.candidate.name %\>  
\<% end %\>  
\<% end %\>

You might find it better to use a has\_many :through, make your models  
look like this: and update your view to the view below

class Job  
has\_many :applications  
has\_many :candidates, :through =\> :applications  
end  
class Candidate  
has\_many :applications  
end  
class Application  
belongs\_to :job  
belongs\_to :candidate  
end

\<% for candidate in @job.candidates %\>  
\<%= h candidate.name %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![Robin\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@Robin\_F](https://www.ruby-forum.com/u/Robin_F)\
**Post date:** [July 21, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/associations-problem/172390/3 "2009-07-21T12:29:11Z")

</div>

2009/7/20 Robin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â has\_many :applications  
> Â \<%= h application.candidate.name %\>  
> trouble but wouldn’t want to change this on a hunch!  
> In [http://wiki.rubyonrails.org/rails/pages/ReservedWords](http://wiki.rubyonrails.org/rails/pages/ReservedWords), application  
> is mentioned as a word that has caused problems.

Colin
