July 6, 2020
Hot Topics:

JRuby on Rails with Nine Lives: Running a JRuby on Rails Application on Tomcat

  • By Dominic Da Silva
  • Send Email »
  • More Articles »

Creating Your Migration

You will use Rails Migrations to manage the creation and deletion of the database table that you are going to use to manage your Guitars from your app. Your table is going to conveniently be named guitars. Run the following from the railswartest direcrory:

jruby script/generate migration add_a_new_table

This will create the db/migrate/001_add_guitar_table.rb file:

Click here for a larger image.

Figure 10: Creating our add_guitar_table migration

This file contains a Ruby class named AddGuitarTable that extends Rails' ActiveRecord::Migration class. Edit this file to contain the following:

class AddGuitarTable < ActiveRecord::Migration
   def self.up
      create_table :guitars do |table|
         table.column :make,  :string, :null => false
         table.column :model, :string, :null => false
         table.column :color, :string, :null => false

   def self.down
      drop_table :guitars

Click here for a larger image.

Figure 11: AddGuitarTable migration class

Now, run the migration:

jruby -S rake db:migrate

Click here for a larger image.

Figure 12: Running your migration to create the guitars table

Running MySQL Query Browser confirms that the guitars table has been created by the migration:

Click here for a larger image.

Figure 13: Empty guitars table in MySQL

Page 4 of 8

This article was originally published on July 31, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date