Creating reports that can be viewed on the desktop is good; creating reports that can be viewed over the Internet is even better. This article will explain how to make reports written with Ruby-on-Rails accessible over the web. It is excerpted from chapter 5 of the book Practical Reporting with Ruby and Rails, written by David Berube (Apress; ISBN: 1590599330). This article is the first part of a two-part series.
Connecting Your Reports to the World - Adding the Data (Page 4 of 4 )
Now that you have a Rails application that can connect to your database and a database structure, you need to fill the database with some data. You can use the SQL code in Listing 5-3 to do just that.
Listing 5-3.Sample Data for the Actor Scheduling Application (actor_schedule_data.sql)
DELETE FROM actors; DELETE FROM projects; DELETE FROM rooms; DELETE FROM bookings;
INSERT INTO actors (id, name) VALUES (1, 'Jim Thompson'); INSERT INTO actors (id, name) VALUES (2, 'Becky Leuser'); INSERT INTO actors (id, name) VALUES (3, 'Elizabeth Berube'); INSERT INTO actors (id, name) VALUES (4, 'Dave Guuseman'); INSERT INTO actors (id, name) VALUES (5, 'Tom Jimson');
INSERT INTO projects (id, name) VALUES (1, 'Turbo Bowling Intro Sequence'); INSERT INTO projects (id, name) VALUES (2, 'Seven for Dinner Win Game Sequence'); INSERT INTO projects (id, name) VALUES (3, 'Seven for Dinner Lost Game Sequence');
INSERT INTO rooms (id, name) VALUES (1, 'L120, Little Hall'); INSERT INTO rooms (id, name) VALUES (2, 'L112, Little Hall'); INSERT INTO rooms (id, name) VALUES (3, 'M120, Tech Center');
Save Listing 5-3 as actor_schedule_data.sql. In the listing, note the use of the DATE_ADD function to create relative times, rather than hard-coding them, so the times will always be in the future. Additionally, note the DELETE FROM statements at the beginning of Listing 5-3. These clear the database before the new data is inserted. For example, this lets you modify the data and then rerun this SQL script to have a fresh copy of your data.
Tip You can also use migration to insert data, but the advantage of inserting your data in a separate SQL file is that it keeps the structure and the data separate. If you had two separate deployments, with two separate databases, you could use the same migrations on both.
You can run the SQL in Listing 5-3 and populate the database using the following command:
mysql -u my_mysql_username -p actor_schedule_development < actor_schedule_data.sql
Creating the Models for the Web Report
The models will represent your tables and the relationships between them. You can create the models using the following commands:
Each of these commands creates a number of files. You now have files named after the tables in the app/models directory, and those are the files you will edit. The generate model command also creates unit tests and fixtures, as well as a migration for each table. You can safely delete the migrations, since you already created all of the tables in your initial migration. The unit test and fixture files are for unit testing; visit the Rails site (http://rubyonrails.org) for information about their use.
First, edit the app/models/actor.rb file so it looks like Listing 5-4.
Listing 5-4.Actor Model (app/models/actor.rb)
clas s Actor < ActiveRecord::Base has_many :bookings end
Save the modified model file. Then edit app/models/booking.rb to look like Listing 5-5.
Listing 5-5.Booking Model (app/models/booking.rb)
class Booking < ActiveRecord::Base belongs_to :actor belongs_to :project belongs_to :room end
The actor and booking models contain all of the relationships you will use in this example. The has_many relationship between the actor and booking tables lets you retrieve all of the booking objects for each actor, and the various belongs_to relationships in the booking model allow you to retrieve the details of each booking. You could also add has_many :bookings relationships to the room and project models, but since you won't use them in the controller, they are omitted.
At this point, you have a database with data in it and a Rails application with a few models. Of course, models are just one-third of an MVC application, so let's create a controller next.
Please check back tomorrow for the conclusion to this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.