Story 5 – Make the away games and home games only upcoming games

After showing the stakeholders the away and home games they realize that this is not what they actually wanted. The new requirement is that we should only show upcoming home and away games on the team page. For now, an upcoming game is any game where the home_score and away_score is null.

Let’s first watch these two rails casts to learn how to use conditions to find data using ActiveRecord:

Task 1: Modify the view so that we only return away_games and home_games that don’t have scores

Task 2: Let’s make our code more reusable using a named scope

Named scopes are really powerful. Watch this rails cast to learn more:

http://railscasts.com/episodes/108-named-scope

Here is a test to validate our named scope, use this to get an upcoming scope working:

When the test pasts, lets use this new upcoming scope in our view instead of specifying the :conditions in the view.


Task 3: Sort games by home team name alphabetically

Anytime we display a list of games we want to sort the games alphabetically by the home_team name. default_scope is a great way to do this once in our model and all the views get the new sorting order. To get this to work we will need to:

  1. Create a default_scope on the game model
  2. Use the option :joins to join the team to a game so we can sort by games using the team name
  3. Use the option :order to specify to sort on the teams.name in DESC order

Rails supports two similar condition options, :joins and :include, to learn more watch this railscast

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS