Find Company Name not ID

I am at a loss why this is not working.....I just want to show the company name if the contact instead of the .id. Any assistance would be grateful.

class Contact < ActiveRecord::Base     belongs_to :company

class ContactsController < ApplicationController   def index     @contacts = Contact.all

    @company = Company.find(@contact.company_id)

and in the view:

<%= @company.company_name%>

I am at a loss why this is not working.....

I'm at a loss as to why you think it should... :slight_smile:

class ContactsController < ApplicationController   def index     @contacts = Contact.all

    @company = Company.find(@contact.company_id)

What would you expect here? @contact above is not defined in the code you're showing us.

Are you trying to display the company name of one contact or all? What is the association between Contact and Company?

Ahh. this works ok in my show view because it looks for only one record for the id. How do I show the correct company name for each contact row in my list table of all contacts which have a belongs_to :company association.

If your associations are correct, you should be able to use e.g. `contact.company.company_name` .

Open up a console and try it.

In the console doing contact = Contact.find(1) finds my contact and the contact.company.company_name returned the correct company name.

Having contact.company.company_name in the index.html file returns an error:

undefined method `company_name' for nil:NilClass

In the console doing contact = Contact.find(1) finds my contact and the contact.company.company_name returned the correct company name.

Having contact.company.company_name in the index.html file returns an error:

undefined method `company_name' for nil:NilClass

You can figure out why that is nil, or you can just guard around it:

contact.company.try(:company_name)

My guess is that this particular contact does not have a company associated with it.

Walter

Unstated assumption: in your "index" page you're doing something like

<% @contacts.each |contact| do %>    <%= contact.company.company_name %> <% end %>

If not, no, of course not, "contact" isn't defined. Cargo-culting example code into your project isn't going to help; you need to think about what it's intended to do.

Using Walters solution has worked. contact.company.try(:company_name)

So my understanding of this now is that because some of the contact records have no company defined (a nil object or NilClass). By using .try it responds nil if no company is found.

So does that mean that if I have a field that uses a foreign key but is not required I should use eg. try(*a, &b)? Or is there another solution?

Thank you.

First, is it OK that a contact has no company, or is that a data error that should have been caught by validations?

If it's OK to have a nil company association, then the question is: how many of these potential error-generating statements do you have? If it's only one or two, you can use the `try` approach above or

1) Use a rescue:      contact.company.company_name rescue ''

2) Test for the existence of company:      if contact.company        contact.company.company_name      end

3) Write a helper to return empty values for nil companies:     # untested example follows :slight_smile:

    company_info_for(contact.company, '') # replaces contact.company.company_name in view

     def company_info_for(company, attr)          defaults = { :company_name => '' }          return defaults'attr] if company.nil?          company.send(attr)      end

Apologies, fat-fingered premature send ...

3) Write a helper to return empty values for nil companies:     # untested example follows :slight_smile:

      company_info_for(contact.company, 'company_name')       # replaces contact.company.company_name in view

     def company_info_for(company, attr)          defaults = { :company_name => '' }          return defaults[attr] if company.nil?          company.send(attr)      end

HTH!