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.
At this point, you've created a database for your application, but before you populate this database, let's connect the application to your database server. After that, you can use a Rails mechanism called migrations to create the database tables for you. Edit the config/database.yml file to read as shown in Listing 5-1.
Listing 5-1.Database Configuration File for the Web Report (config/database.yml)
Note that Rails creates three database connection settings by default, but this example uses only the development environment to keep things simple. The other environments are testing, used for automated testing, and production, used for deployment. Additionally, the default file includes a number of comments, which I've removed from the listing for the sake of brevity.
Next, you need to create a new migration.
Creating a Migration
Migrations are bits of Ruby code that control the structure of a database. Each migration represents a set of changes to a database. The first migration usually specifies the initial structure of a database, and each successive version represents a change of some kind--adding a column, setting a default value, renaming a table, and so forth. Migrations are designed to be cross-platform, so you can usually run the same migration across multiple databases. (Of course, if you use any database-specific features, the migration won't be cross-platform.) Migrations are versioned, so you can upgrade and downgrade them as you see fit. Rails also keeps track of the current version of your database, so if you have a number of migrations, Rails will run only the new migrations.
Create a new migration by using the following command:
This creates a skeleton migration. The initial db/migrate/001_initial_schema.rb file looks like this:
class InitialSchema < ActiveRecord::Migration def self.up end
def self.down end end
As you can see, it's a single class that inherits from ActiveRecord::Migration, and it has two class methods: up and down, which upgrade and downgrade the version of the database structure, respectively, when the migration is run. Fortunately, Rails creates the skeleton containing the class definition and method names, so you only need to fill out the migration. Let's do that now. Listing 5-2 shows the full migration class.
Note that this process also works in reverse. You can specify a version to migrate your database to using the VERSION=x option, and specifying 0 will revert your database. For example, the following command will undo the previous migration command:
Note The rake command has a whole host of other uses. It's similar to the make command, but it's written in pure Ruby and is used to perform various maintenance tasks for Rails applications. The particular rake task discussed here, db:migrate, uses migrations to either upgrade or downgrade a database. You can find a list of all of the tasks available for a Rails application by running the command rake -T.