Feeds

Ruby on Rails

Simpler than J2EE

Top 5 reasons to deploy VMware with Tegile

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

Remote control for virtualized desktops

More from The Register

next story
Be real, Apple: In-app goodie grab games AREN'T FREE – EU
Cupertino stands down after Euro legal threats
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
You stupid BRICK! PCs running Avast AV can't handle Windows fixes
Fix issued, fingers pointed, forums in flames
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Facebook, working on Facebook at Work, works on Facebook. At Work
You don't want your cat or drunk pics at the office
Soz, web devs: Google snatches its Wallet off the table
Killing off web service in 3 months... but app-happy bonkers are fine
First in line to order a Nexus 6? AT&T has a BRICK for you
Black Screen of Death plagues early Google-mobe batch
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.
Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
5 critical considerations for enterprise cloud backup
Key considerations when evaluating cloud backup solutions to ensure adequate protection security and availability of enterprise data.
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.
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.