【发布时间】:2012-12-16 06:57:30
【问题描述】:
我有两个型号team 和fixture。 fixture 模型有两列home_team_id 和away_team,其中包含team 的主键的外键。
我正在尝试进行选择查询,我可以使用夹具表中的home_team_id 和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>
【问题讨论】:
-
您的
Team类中有2 个has_many :fixtures关系,这可能是个问题... -
您的查询显示为
FROM teams,但您的对象名为team。无论如何,您是否收到了一些错误消息? “没有运气”并没有告诉我们太多......
标签: mysql sql ruby-on-rails ruby select