ahmet
ahmet

Reputation: 5005

How to get the name from an ID in another table

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

I want to get the company_id's name somehow from the other table.

I've tried <%= contact.company_id.collect(&:name) %>

without success can i get some help here?

Upvotes: 0

Views: 2864

Answers (1)

fl00r
fl00r

Reputation: 83680

# Models
class Contact < ActiveRecord::Base
  belongs_to :company
end

class Company < ActiveRecord::Base
  has_many :contacts
end

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

Upvotes: 7

Related Questions