This article is more than 1 year old

Ruby on Rails

Simpler than J2EE

Creating a Database Table

In this article we shall develop a Rails application with CRUD (Create, Read, Update, Delete) functionality. The example application requires a database table, which we shall create in this section. A database table may be created using ActiveRecord migrations. A migration is a class that extends the ActiveRecord::Migration class and is run with the rake command. First, we need to configure the database.yml configuration file in the config directory of the example Rails application, rubyrails, to use the Oracle database. A migration runs in the development environment by default, but may also be run in production environment or test environment - modify the development environment settings in database.yml file for the Oracle database to as shown in following listing.

 development:
 adapter: oci
  database: ORCL
  username: OE
  password: password
  host:

The host value should be kept empty and the space between the ‘:’ and the configuration values is required: for example, specify adapter: oci instead of adapter:oci.

A migration script may be created with the following command:

 c:/ruby>ruby script/generate migration migrationname

Variable migrationname specifies the migration name. A migration may also be created by creating a model script, which also creates a migration script. As we shall be creating a MVC application, the migration script will be created by generating a model script. To create a model script, catalog.rb, Cd(change directory) to the rails application root directory, rubyrails, and run the following command:

 C:>ruby>rubyrails>ruby script/generate model catalog

Model class script catalog.rb gets generated in the models sub directory of the app sub directory. A migration script, 001_create_catalogs.rb , which consists of CreateCatalogs class gets generated in the migrate sub directory of the db sub directory. The migration class, CreateCatalogs, extends the ActiveRecord::Migration class. The default migration script is listed below:

 class CreateCatalogs < ActiveRecord::Migration
  def self.up
    create_table :catalogs do |t|     
     # t.column :name, :string   
    end
  end
  def self.down
   drop_table :catalogs
  end
end

A default migration consists of actions self.up and self.down. Method self.up consists of Ruby code to implement the migration and self.down consists of Ruby code to rollback the migration. In the CreateCatalogs class, self.up consists of transformation create_table, used to create a catalogs table. ActiveRecord uses pluralisation to map a model class to a database table. The model class is singular and capitalized and the database table is plural and lowercase (for example, if the model class is Catalog, the table name is catalogs). The self.down method in CreateCatalogs class consists of a drop_table transformation to drop database table catalogs. A migration class may define migration transformations, as discussed in following table:

Transformation Description
create_table(name, options) Used to create a table.
drop_table(name) Used to drop a table.
rename_table(old_name, new_name) Used to rename a table.
add_column(table_name, column_name, type, options) Used to add a column to a table.
rename_column(table_name, column_name, new_column_name) Used to rename a table column.
change_column(table_name, column_name, type, options) Used to change a column to a different type.
remove_column(table_name, column_name) Used to remove a column.
add_index(table_name, column_name, index_type) Adds an index.
remove_index(table_name, column_name) Removes an index.

Next, we’ll modify the migration class, CreateCatalogs, to create a table, add columns to the table and initialize the table with data. To the catalogs table add columns journal, publisher, edition, title, author of type string and size 255. The example migration script uses the block form of create_table.

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
   end

Column types that may be added are integer, float, datetime, timestamp, time, text, string, binary and boolean. Add data to the catalogs table with ActiveRecord::Base class method create. An example row is added as shown below:

 Catalog.create :journal => "Oracle Magazine",
 :publisher => "Oracle Publishing",
 :edition => "Nov-Dec 2004",
 :title=> "From ADF UIX to JSF",
 :author=>"Jonas Jacobi"

The complete migration script to create example database table catalogs is listed in following listing:

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
   end
   
Catalog.create :journal => "Oracle Magazine",
 :publisher => "Oracle Publishing",
 :edition => "Nov-Dec 2004",
 :title=> "From ADF UIX to JSF",
 :author=>"Jonas Jacobi"

Catalog.create :journal => "Oracle Magazine",
 :publisher => "Oracle Publishing",
 :edition => "Nov-Dec 2004",
 :title=> "Database Resource Manager",
 :author=>"Kimberly Floss"


  end

  def self.down
  drop_table :catalogs
  end
end

Next, run the migration with rake; Rake is similar to Java’s ant. Rails has a target called migrate to run migrations. Change directory (Cd) to the rubyrails directory and run the following command:

c:/ruby/rubyrails>rake migrate

This generates an Oracle database table called catalogs along with a sequence catalogs_seq. This will be used in the next Ruby on Rails tutorial, where we’ll create a MVC CRUD application &reg:.

More about

TIP US OFF

Send us news


Other stories you might like