Bear with me as I'm a Rails noob. I have a basic league system. League -> Seasons -> Divisions -> Teams. I would like my Divisions and Teams to belong to any number of seasons. My models are as follows...
class Season < ActiveRecord::Base
belongs_to :league
has_many :season_division_teams
has_many :divisions, :through => :season_division_teams
end
class Division < ActiveRecord::Base
belongs_to :league
has_many :seasons, :through => :season_division_teams
has_many :season_division_teams
has_many :teams, :through => :season_division_teams
end
class Team < ActiveRecord::Base
belongs_to :league
has_many :season_division_teams
has_many :seasons, :through => :season_division_teams
has_many :divisions, :through => :season_division_teams
end
class SeasonDivisionTeam < ActiveRecord::Base
belongs_to :season
belongs_to :division
belongs_to :team
end
What I want to be able to do ultimately is say given that i'm in season '1', what divisions do I have, and then given those divisions what teams are part of those divisions (for the given season).
Season.Find(1).divisions.all do |division|
# Do something with the teams that make of the division (for that season)
# division.teams.count
end
I just can't get my head around how to do this in Rails, which seems very simple to me in SQL.
Thanks in advance for the help.
Edit:
Just to clarify, I'm trying to get the teams for a given season (with their divisions). With my model I know I can get the divisions for a given season. I just need to the the teams for those divisions and season.
Thanks again.