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

Choosing a cloud hosting partner with confidence

More from The Register

next story
Microsoft to bake Skype into IE, without plugins
Redmond thinks the Object Real-Time Communications API for WebRTC is ready to roll
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
Mozilla: Spidermonkey ATE Apple's JavaScriptCore, THRASHED Google V8
Moz man claims the win on rivals' own benchmarks
FTDI yanks chip-bricking driver from Windows Update, vows to fight on
Next driver to battle fake chips with 'non-invasive' methods
PEAK APPLE: iOS 8 is least popular Cupertino mobile OS in all of HUMAN HISTORY
'Nerd release' finally staggers past 50 per cent adoption
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Ubuntu 14.10 tries pulling a Steve Ballmer on cloudy offerings
Oi, Windows, centOS and openSUSE – behave, we're all friends here
Was ist das? Eine neue Suse Linux Enterprise? Ausgezeichnet!
Version 12 first major-number Suse release since 2009
prev story

Whitepapers

Why cloud backup?
Combining the latest advancements in disk-based backup with secure, integrated, cloud technologies offer organizations fast and assured recovery of their critical enterprise data.
A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Top 5 reasons to deploy VMware with Tegile
Data demand and the rise of virtualization is challenging IT teams to deliver storage performance, scalability and capacity that can keep up, while maximizing efficiency.
Mitigating web security risk with SSL certificates
Web-based systems are essential tools for running business processes and delivering services to customers.