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:
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.
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:
Using the find method, run statements that do the following:
Using ActiveRecord methods do the following:
Before you do these exercises, you might want to go through the tutorial on Ruby lists.
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
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.
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:
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.
This project is worth 20 points:
Submissions are eligible for partial credit. Always submit an assignment even if it is not complete.