trying to refactor code to provide clean association
A GAME has a HOME_TEAM and an AWAY_TEAM
A TEAM has many GAMES as a HOME_TEAM or an AWAY_TEAM
Association between GAME and TEAM is a straight-forward HABTM BUT I need to denote which of the two TEAMS associated with a GAME is the HOME_TEAM and which is the AWAY_TEAM. I did it by adding extra fields and associations but this is obvious very wet rather than dry. I know the answer is in through but I seem to have had a brain meltdown and can’t quite get my head round this.
Basically I want to be able to do Game.teams (returns collection of both teams) and Game.home_team (get and set a TEAM to home_team) and Game.away_team (get and set a TEAM to away_team)
Sorry to pose such a straightforward sounding query but it’s just got away from me
class Game < ActiveRecord::Base
belongs_to :home_team
belongs_to :away_team
has_and_belongs_to_many :teams
end
class HomeTeam < ActiveRecord::Base
belongs_to :team
has_one :games
end
class AwayTeam < ActiveRecord::Base
belongs_to :team
has_one :games
end
class Team < ActiveRecord::Base
has_and_belongs_to_many :games
has_many :away_teams
has_many :home_teams
end
All help greatly appreciated
Peter
To do what you want to do, you should use a
has_many :throughinstead ofhatbm. See here for more info. In short, the good thing is that you can add other variables to the joins table. In your case, a boolean called home_team.So here’s what I’d do. First, create an association table (since I don’t have much imagination, I’ll call it participation):
As you can see, unlike your gamesteams table, this one has an id. And you can add attributes to it.
Then, I would use these models:
So to get the teams of a game, you do
@game.teams.Now, to get home_team and away_team, add these methods to your Game model:
And then you’ll be able to do
@game.home_teamand@game.away_team.I think for false you should try using
where("participants.home_team = ?", false)Ok, so so there are at least 2 ways to set up your teams.
If you go for number 1, you should use a radio button to let the user decide. Something like this:
So if
params[:home_team] == 1, the first team is the home team, ifparams[:home_team] == 2, the second team is the home team.If you go for number 2, then, you should have something like this in your form do add the teams to your game:
So then in your controller you can do something like