Rails and Databases
Ruby on Rails provides a model-view-controller (MVC) framework for Web application development. The "model" portion of the application can be persisted in a relational database. Rails supports a variety of RDBM systems including MySQL, PostgreSQL, SQLite, IBM DB2, Oracle, Microsoft SQL Server, and Sybase Adaptive Server (click here
for more information on Rails supported databases and available drivers). Pure Ruby database drivers are provided with the Rails download for MySQL, PostgreSQL, and SQLite. For the purposes of this article, MySQL access was obtained by using the Rails' provided Ruby driver. You will get more information on configuring Rails to use the database and the supporting Ruby on Rails API for wrapping the database in a bit. However, before beginning the development of your first Ruby on Rails application, you will need a database that contains a few database tables. Specifically, through this guide, you will build a personal contact application aptly called "Ruby Rolodex." For this first Ruby on Rails application, you will need a set of database tables created from the model shown in Figure 1
|Figure 1. Ruby Rolodex Database Model: Ruby on Rails provides a convenient means to access your relational database. In the sample application below, this model is used to persist contact information.|
I implemented the database in MySQL and the SQL code to restore the database using MySQL Administrator's backup restore is included in the download zip file under the SQL folder.
With the Rails framework and a database in place, you are ready to create your first Ruby Web application. Don’t blink and miss all the fun!
Rails Project Creation and Rails Database Configuration
To say that you will be writing your first Rails Application isn’t quite accurateRuby on Rails provides a number of metaprogramming generators (scripts) that build a lot of your Web application for you. How much coding you do depends on how much you want to accept the default behavior and look/feel provided by the metaprogramming scripts.
Figure 2 shows the "Ruby Rolodex" application you'll be creating will present users with a list of family, friend, business, and coworker contacts stored in a relational database. The application will also allow users to collect and store new contacts, remove old contacts, and even re-categorize the contacts.
|Figure 2. Ruby Rolodex Application: The Ruby Rolodex application allows users to create, look up, edit, or remove contacts and a contact’s address.|
To begin, execute the Ruby on Rails script that creates a new Web application project. Locate a folder on your system where you want to create the Rails project and store all of its associated files. Figure 3 shows a new "rolodex" project folder on the root C drive. From the directory in which you want to create the project folder, issue the following command to create the new "Ruby Rolodex" project: rails rolodex.
This command executes an already provided Ruby on Rails script that creates the entire Web application folder structure and necessary configuration files. Looking at the project folder, in this case the rolodex folder, you will find a set of folders and Rails script files as depicted in Figure 4.
With the application structure in place, you need to configure your Rails application to point to the database; in my case the database is in MySQL. Open the database.yml file located in the \rolodex\config folder with your favorite text editor. Note that this configuration file already has database bindings for MySQL, PostgreSQL, and SQLite. You might also take note of the fact that the configuration file has already been set up to utilize a "production," "test," and "development" database. To get the application to use the rolodex_development schema on a MySQL database with username and password of "root," I had to modify the file as shown below:
With that small change, Ruby on Rails is ready to access your database and help you build other parts of your application.
Figure 3. Executing Rail Project Script: Running the "rails rolodex" command creates a new project folder called rolodex at the location where you run the script. In this case, the rolodex folder is created right on the C: root directory.
Figure 4. Ruby Rolodex Application Folder: Running the “rails rolodex” command creates an entire project structure containing a set of folders and configuration files as shown here.