I try to build a soccer manager game using rails and I’m stuck with the rails association.
team table
...
history table
team_id
saison_id
saison table
league_id
league table
...
I want to find all the team for one league.
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
I want to do something like (in the show method of the league controller) :
@team = Team.find(:all, :include => [:saisons => :histories, :leagues => :saisons],
:conditions => ["leagues.id = ?", params[:id]])
but it does not work.
The SQL query :
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
this query return all the team for one saison…but I do not achieve to make it work for league
@division = Team.find(:all, :include => [:saisons => :histories],
:conditions =>["saisons.id =?",params[:id]])
In the league controller, in the show method, I try something like
I would include the league in the find statement to have all its attributes (name for instance) in the same result tab.
I know I can those information about league in another query, but it is not the best way to do it.