开发者

How to get the name from an ID in another table

开发者 https://www.devze.com 2023-03-28 19:13 出处:网络
<% @contacts.each do |contact| %> <%= contact.company_id %> <% end %> I want to get the company_id\'s name somehow 开发者_如何转开发from the other 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?


# 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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消