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
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 %> On Jul 20, 2:55 pm, Robin Fisher <robinjfis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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
2009/7/20 Robin Fisher <robinjfisher-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > 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!In http://wiki.rubyonrails.org/rails/pages/ReservedWords, application is mentioned as a word that has caused problems. Colin