Ruby on rails 通过Rails中的外键从表中获取值?

Ruby on rails 通过Rails中的外键从表中获取值?,ruby-on-rails,datatable,foreign-keys,show,relationship,Ruby On Rails,Datatable,Foreign Keys,Show,Relationship,嗨,我创建了两个模型 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

嗨,我创建了两个模型

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
class Fixtureteam
结束
类团队Fixture,:foreign\u key=>:home\u team\u id
有很多:Fixture,:class\u name=>Fixture,:foreign\u key=>:away\u team\u id
结束
在fixtures表中,我将团队id存储在home\u team\u id和away\u team\u id列中

然后在我的fixtures/show.html.erb中显示存储的id

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

主队:

如何通过获取fixtures表中存储的team.id来显示team表中的team.name

我需要将此行更改为其他内容,但不确定是什么?


使用
归属
您可以使用:
@fixture.team
访问它,因为您定义了一对多关系,所以它不应该同时具有
主队id
客队id
,并将访问相应的。

我尝试了@fixture.team.team\id(fixture.home\u team\u id).name给了我一个错误。我如何使用fixtures表中home\u team列中的值调用团队?哦,您还需要使用
所属项目中的
外键
选项。因此,您需要2个
属于
语句,例如
属于:home\u team,:class\u name=>team,:foreign\u key=>:home\u team\u id
,并作为
@fixture.home\u team
访问它们