Feeds

Ruby on Rails

Simpler than J2EE

Secure remote control for conventional and virtual desktops

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:.

The essential guide to IT transformation

More from The Register

next story
Microsoft boots 1,500 dodgy apps from the Windows Store
DEVELOPERS! DEVELOPERS! DEVELOPERS! Naughty, misleading developers!
'Stop dissing Google or quit': OK, I quit, says Code Club co-founder
And now a message from our sponsors: 'STFU or else'
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Uber, Lyft and cutting corners: The true face of the Sharing Economy
Casual labour and tired ideas = not really web-tastic
Mozilla's 'Tiles' ads debut in new Firefox nightlies
You can try turning them off and on again
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
prev story

Whitepapers

5 things you didn’t know about cloud backup
IT departments are embracing cloud backup, but there’s a lot you need to know before choosing a service provider. Learn all the critical things you need to know.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Backing up Big Data
Solving backup challenges and “protect everything from everywhere,” as we move into the era of big data management and the adoption of BYOD.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?