2012-12-13 79 views
0

您好我創建了兩個模型,通過Rails中的外鍵從表中獲取值?

class Fixture < ActiveRecord::Base 
attr_accessible :away_score, :away_team_id, :home_score, :home_team_id, :result, :week 
belongs_to :team, :class_name => Team 
end 

class Team < ActiveRecord::Base 
attr_accessible :form, :name 
has_many :fixtures, :class_name => Fixture, :foreign_key => :home_team_id 
has_many :fixtures, :class_name => Fixture, :foreign_key => :away_team_id 
end 

在我的燈具表我存儲在home_team_id和away_team_id列TEAM_ID。

然後在我的燈具/ show.html.erb我顯示ID存儲

<p> 
    <b>Home team:</b> 
    <%= @fixture.home_team_id %> 
</p> 

我如何可以顯示通過獲取存儲在燈具表team.id從球隊表team.name?

我需要改變這一行<%= @ fixture.home_team_id%>其他東西,但不知道是什麼?因爲你定義一個一對多的關係@fixture.team它決不應同時具有home_team_idaway_team_id並訪問相應的一個:

回答