IT 231 Web Development I
Fall 2011

Assignment 2
Database Access with Ruby Commands
Due Sunday September 25 by 11:30 PM

Overview

For this project, you will practice writing Ruby statements that access the database to your web application. The goals for the assignment include learning the following:

  1. Scaffold creation with model validations
  2. Basic Ruby syntax and statements
  3. Objects and methods
  4. Object Relational Mapping (ORM) using ActiveRecord
  5. Editing Ruby statements in a script file

Scaffold creation

Create a simple scaffold application with three or four attributes. You may choose the theme, but use something other the airports or movies. Likely types for your attributes include integer, string, text (creates a text area), decimal, datetime, time, boolean, float and double. When naming attributes, be careful not to use any of the special column names (e.g. type) when naming an attribute. These special names can be found at the bottom of page 269 to the top of page 270.

Go to the model file for your application located in app/models/your_model_name.rb. Add two or three meaningful validations. Here are some examples that would be good for the Airport model (with city and code):

   validates :city, :presence => true
   validates :code, :uniqueness => true, :length => 3..3

You can find more examples in the book (pp. 75 - 77) and online.

Using the scaffolded application, add a few records. Test whether the validation statements work.

Database commands

For these exercises, you will need to open a Command Prompt (Terminal window on Macs) and run rails console. From there, perform the following operations, making sure you note the commands you typed:

  1. Accessing a single record
  2. Using the find method, run statements that do the following:

    1. List a table record by its id.
    2. List a table record that has a particular field value.
    3. List a table record that meets a specific condition.
  3. Creating, updating and deleting records
  4. Using ActiveRecord methods do the following:

    1. Create a new data model object and save it in the database.
    2. Retrieve a record, change one of its fields and update it in the database.
    3. Delete a record in the database.
  5. Accessing and working with a list of records
  6. Before you do these exercises, you might want to go through the tutorial on Ruby lists.

    1. Display the list of all of your table records.
    2. Display the list showing only one field (e.g. name, title)
    3. Display the list so that the table items are sorted by a specific field.
    4. Display the list so that it only shows records that meet a specific condition.

Report Script

Write a Ruby script that provides a summary report of the records in your application's database. At minimum, the report should produce the following:

You are strongly encouraged to add additional information to your summary report. Examples might include the date of when the last record was added or a listing showing records that meet a specific condition.

Name the file of your script report.rb and place it in your rails application folder. You can then run your report by typing in the following command: rails runner report.rb

Below is an example script for the Airport model:

num_airports = Airport.count
puts "The database currently has #{num_airports} airports stored in it."

puts

list = Airport.order("code")
puts "Codes listed in alphabetical order"
list.each do |airport|
  if airport.code != ""
    puts airport.code
  end
end

puts

list = Airport.where(:code => "")
if list.length > 0
  puts "These cities have no codes:"
  list.each do |airport|
    puts airport.city
  end
else
  puts "All of the cities have codes"
end

Seed file

Write a ruby script that deletes all of the records in the database and then saves a few records to seed the application. Call your file seed.rb. Here is an example script using the Airport model.

Airport.delete_all

a = Airport.new
a.city = "Chicago"
a.code = "ORD"
a.save

a2 = Airport.new
a2.city = "Cleveland"
a2.code = "CLE"
a2.save

For an extra challenge, have your seed script read from a text file or ask a user for record information.

Report

Using any word processor, write a short summary document that describes your scaffolded application including the validations (1 or 2 paragraphs). The summary document should include the following:

Submission

Place the summary document and the two scripts in your rails application folder (if they aren't there already). Zip up the folder. Submit the zip file using D2L.

Grading

This project is worth 20 points:

Submissions are eligible for partial credit. Always submit an assignment even if it is not complete.