Week 5 "Extinctions" Assignment

We've got an uplifting project this week, web app scientists :) You'll be creating a database of animals from North and South America that went exctinct during the holecene extinction (past 12,000 years to present).

Extinctions: seeds.rb (save to db/seeds.rb)

Extinctions: holocene-americas.csv (save to db/holocene-americas.csv)



Setting up the data

  1. Create a new Rails app called extinctions
  2. Initialize a git repository (in the extinctions) directory and commit all the files (a good commit message would be something like "Setup Rails project")
  3. Generate a model called Classification with the following fields in its migration:
    • name (string)
  4. Generate a model called Animal with the following fields in its migration:
    • classification (references)
    • name (string)
    • range (string)
    • url (string)
    • image_url (string)
    • extinction_date (integer)
  5. Add a recent? instance method to the Animal model (class). It should return true if extinction_date is after 1900, and false otherwise.
  6. Generate a model called CommonName with the following fields in its migration:
    • animal (references)
    • name (string)
  7. Add the associations to the files in the app/models directory (both belongs_to and has_many)
    • Classification has many Animals, Animals have many CommonNames
  8. Add your files and commit with git (a good commit message would be something like "Added models")
  9. Run the migrations
    (If you get messed up at this point you can clear everything in the database with bin/rake db:drop db:create db:migrate)
  10. Copy in seed data in db/seeds.rb and db/holecene-americas.csv (linked above) and load the seeds (bin/rake db:seed)
  11. Add your files and commit with git (a good commit message would be something like "Added seed data")

Setting up the Classifications controller and views

  1. Generate a Classifications controller and add it to your routes.rb file
    • Add an index method that retrieves the list of Classification records
    • Add an index view that displays:
      • each Classification name
      • with the number of animals that are included (e.g. "Mammalia (10 animals)")
      • a link to the ClassificationsController#show method
    • Add a show method and view
    • The show view should display
      • the current Classification name at the top of the page
      • links to each animal – these links should point to the AnimalsController#show route
      • the animals should be sorted by exctinction date, oldest to newest (do this in the controller)
      • a picture of each animal (but only if it has one) – you'll want to use the image_tag method
      • whether the extinction was recent (use your recent? method)
  2. Add a root route that points to the Classifications index view
  3. Add your files and commit with git (a good commit message would be something like "Added Classifications controller and views")

Setting up the Animals controller and views

  1. Generate an Animals controller and add it to your routes.rb file
    • Add a show method and view file to display an individual animal
    • On the animal's show page display
      • the animal's scientific name
      • the animal's common names (if any)
      • display its image (but only if it has one) – you'll want to use the image_tag method
      • whether the extinction was recent (use your recent? method)
      • link to the source URL
  2. Add your files and commit with git (a good commit message would be something like "Added Animals controller and views")

Optional: nicer URLs

  1. Figure out what Rails' to_param method does an use it to include record names in your URLs
  2. Read up on nested routes and figure out how to nest the :animals resource under the :classifications resource

If you do both of these things, you should be able to have URLs like /classifications/1-mammalia/animals/123-bison-priscus



Submit your work

When you are done, push your work to Github. Then email me a link to your repository on Github: alex@dunae.ca