我有两个模型team
和fixture
. 该fixture
模型有两列home_team_id
,away_team
其中包含team
主键的外键。
我正在尝试进行选择查询,我可以在其中使用夹具表中的home_team_id
and获取团队名称。away_team_id
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
我是否需要在 Fixtures 控制器中执行 SQL 查询,然后如何在我的fixture
视图中显示它?
这是我尝试查询但没有运气。在我的灯具秀中,我有:
<p>
<b>Home team:</b>
<%= Team.find_by_sql("SELECT name FROM teams WHERE team.id = fixtures_home_team_id") %>
</p>