Story 3 – Link to show a team page from game index page
When viewing the index page of games to see all games, lets link to the team page for the team clicked. Remember that right now our games and teams are not associated so we will have to do more then just add html to make this work.
Task 1 – Create some seed data
http://railscasts.com/episodes/179-seed-data
http://asciicasts.com/episodes/179-seed-data
- Run rake db:seed
Task 2 – Create a link_to for the teams in our game index page
What happens when you view http://localhost:3000/games ?
Task 3 – Migrate the database schema AND data to fit your associations
Our database stores the home_team and away_team in a string, we will need to rename these columns using a migration. Before we modify the schema we have to migrate all our existing game data. Create a migration that does the following:
- Add two new columns to store the home team’s id and away team’s id
- Retrieves all the games from your database
- For each game:
- find the Team model for the away team by the name stored in the away_team property
- find the Team model for the home team by the name stored in the home_team property
- If the team does not exist in the database we should create the team now
- Save the game we change
- Remove the old home_game and away_game columns
Our test from Task 2 should now work. Make sure by running rake test
Task 4 – Associate the game model to the Team
A game belongs_to an away team and belongs to a home team. If we do belongs_to :home_team rails expects there to be a model called HomeTeam. This means we need to give rails a few more hints so that home_team returns an actual team model. Lets read up on the belongs_to method a little more:
- Read section 3.3.1 Creating Foreign Keys for belongs_to Associations in the RailsGuides on associations
- Read about all the options for the belongs_to method in RailsBrain
So lets add the proper options so we can get belongs_to to work, here is a test that should pass when we complete this task correctly:
Task 5 – Modify the create action so the team name is a Team model before saving
The form on the game’s create and edit actions still allows the user to to type in a team name but if you try and saving this form now we will get an error. This is because params[:home_team] still has a string but the Game model now expects a Team model for both the home_game and the away_game. Let’s fix our game controller’s create action to take the string from the form and convert it to a model before passing the params to Game.create .
After we get the create action working lets do the same for the edit. We will need to modify the form html for the edit to work because now home_team returns a model and not a string. When the form renders the game it calls to_s on the team model and puts that in the text field instead of the team name. Pass the :value you want the home_team and away_team text fields to display when editing a game.
After you get the edit action working, now go back and make sure the new still works.
Task 6 – DRY up your controllers using a before_filter
Having duplicate code in the create and update action doesn’t seem right. Lets use controller filters to remove the duplication. Documentation can be found at RailsBrain.com
Here is what I did:
Also note that ActiveRecord models also have callbacks similar to controllers. Read about them here