mercredi 9 décembre 2015

Displaying values from another model in view

I'm new to this and have an easy question I'm sure. I have 2 models, customers (has many jobs) and jobs (belongs to customers). I have a view which displays job details. I want to select data from the customer, but am getting an error.

Jobs index method in controller:

@jobs = Job.all (NB have also tried @jobs = Job.includes(:customer).all)

In my view:

<% @jobs.each do |job| %> ... <td><% job.customer.surname %></td>

But I get the following error:

undefined method `surname' for nil:NilClass

What am I doing wrong please?

Thanks!

Aucun commentaire:

Enregistrer un commentaire