Sun GlassFish Enterprise Server v3 Scripting Framework Guide

ProcedureTo Create a Database-Backed Rails Application

  1. Create or select a directory for creating a database-backed Rails application.

  2. Change to that directory.

  3. Create and configure a application template to use the MySQL database:

    jruby -S rails books -d mysql
  4. Change to the books directory that you just created.

  5. Replace mysql with jdbcmysql.

  6. Open the config/database.yml file in a text editor.

  7. When prompted, enter your MySQL root password under the development heading in the database.yml file.

  8. Change back to the books directory if you are not already there.

  9. Create the database by running the following command:

    jruby -S rake db:create

    After the database creation is complete, you should see output similar to the following:

    ** Execute db:create

    The rake command invokes the Rake tool. The Rake tool builds applications by running Rake files, which are written in Ruby and provide instructions for building applications.

  10. Create the scaffold and the Book model for the application:

    jruby script/generate scaffold book title:string 
    author:string isbn:string description:text

    When you run the script/generate command, you specify the name of the model, the names of the columns, and the types for the data contained in the columns.

    A scaffold is the set of code that Rails generates to handle database operations for a model object, which is Book in this case. The scaffold consists of a controller and some views that allow users to perform the basic operations on a database, such as viewing the data, adding new records, and editing records. Rails also creates the model object when generating the scaffold.

  11. Create the database tables:

    jruby -S rake db:migrate

    When Rails is finished creating the tables, you should see output similar to the following:

    CreateBooks: migrated (0.1322ms) =========

    If you need to reset the database later, you can run the following command:

    jruby —S rake db:reset