私は2つのモデルteam
とを持っていfixture
ます。fixture
モデルには2つの列がhome_team_id
あり、主キーaway_team
の外部キーが含まれていteam
ます。
フィクスチャテーブルのhome_team_id
とを使用してチーム名を取得できるselectクエリを実行しようとしています。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
フィクスチャコントローラでSQLクエリを実行する必要がありますか?それをfixture
ビューに表示するにはどうすればよいですか?
これは私がクエリに対して試したものですが、運がありませんでした。私の備品ショーで私は持っていました:
<p>
<b>Home team:</b>
<%= Team.find_by_sql("SELECT name FROM teams WHERE team.id = fixtures_home_team_id") %>
</p>