开发者

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?


# 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜