Ruby on Rails Meets Eclipse
Pages: 1, 2, 3, 4, 5

Creating a Database Table

First, we need to modify the database.yml file for the MySQL database. Modify the development configuration with the following settings, as shown in Figure 9.

  adapter: mysql
  database: test
  username: root
  host: localhost

Modifying database.yml
Figure 9. Modifying database.yml

Next, we will create a database table in the MySQL database using migrations, for which we need to create an external tools configuration for the rake command. Rake is similar to Java's ant command and is used to run the migrate target. Specify rake.bat in the Location field of the Rake configuration. Specify {project_loc} in the Working Directory field and migrate in Arguments.

Rake Configuration
Figure 10. Rake configuration

We will create a migration script by creating a model script, which also creates a migration script. Select Run>External Tools>Create Model to create a model script. In the Variable Input frame, specify catalog as the model name and click on OK. A model script, catalog.rb, and a migration script, 001_create_catalogs.rb, get added to the Rails project catalog. Modify the migration script to create a database table, "catalogs." Migration script 001_create_catalogs.rb is listed as follows:

class CreateCatalogs < ActiveRecord::Migration
  def self.up
    create_table :catalogs do |t|
   t.column :journal, :string, :limit => 255
   t.column :publisher, :string, :limit => 255
   t.column :edition, :string, :limit => 255
    t.column :title, :string, :limit => 255
   t.column :author, :string, :limit => 255


Catalog.create :journal => "developerWorks", 
:publisher => "IBM", :edition => "September 2006", 
:title=> "A PHP V5 migration guide",:author=>"Jack D. Herrington"
Catalog.create :journal => "developerWorks", 
:publisher => "IBM", :edition => "September 2006", 
:title=> "Make Ruby on Rails easy with RadRails and Eclipse",
:author=>"Pat Eyler"
  def self.down
    drop_table :catalogs

Start the MySQL database, if it is not already started, and run the migration with the rake command. Select the migration script and select Run>External Tools>Rake. The database table "catalogs" gets created, as shown in Figure 11.

Creating Database Table
Figure 11. Creating a database table

Pages: 1, 2, 3, 4, 5

Next Pagearrow