我尝试使用轨道构建足球经理游戏,并且我与轨道关联卡住了。在轨道中使用多个连接找到
team table
...
history table
team_id
saison_id
saison table
league_id
league table
...
我想找一个联盟的所有球队。
class History < ActiveRecord::Base
belongs_to :saison
belongs_to :team
end
class Saison < ActiveRecord::Base
has_many :histories
has_many :teams, :through => :histories
belongs_to :league
end
class League < ActiveRecord::Base
has_many :saisons
end
class Team < ActiveRecord::Bases
has_many :histories
has_many :saisons, :through => :histories
end
我要像做(在联赛控制器的显示方法):
@team = Team.find(:all, :include => [:saisons => :histories, :leagues => :saisons],
:conditions => ["leagues.id = ?", params[:id]])
,但它不工作。
SQL查询:
SELECT Team.*
FROM Team, Saison, History, League
WHERE History.Team_ID = Team.ID AND
History.Saison_ID = Saison.ID AND
Saison.League_ID = League.ID
该查询返回所有球队一个赛松...但我不实现,使之成为联赛
@division = Team.find(:all, :include => [:saisons => :histories],
:conditions =>["saisons.id =?",params[:id]])
我的数据库是用法语提出的,我用英文翻译了一些字段,但似乎我犯了翻译错误。我编辑关于你所说的话的线程。谢谢 – psic 2012-01-31 21:16:24