【发布时间】:2012-01-31 20:24:56
【问题描述】:
我尝试使用 Rails 构建足球经理游戏,但我被 Rails 协会困住了。
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
我想做类似的事情(在联赛控制器的 show 方法中):
@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]])
【问题讨论】:
标签: ruby-on-rails join associations