Oracle GlassFish Server 3.0.1 Scripting Framework Guide

Accessing a Database From a Rails Application

One of the main strengths of Rails is that it makes it easy to create applications that access databases. This section explains how to create a simple application that accesses a book database using MySQLTM.

ProcedureTo Set Up the MySQL Database Server

Before You Begin

JRuby, Rails, and the required Gems should already be installed, as described in Installing JRuby and Rails.

  1. Download and install MySQL database server:

    MySQL Server is available from the MySQL Downloads page. MySQL installation instructions are available from the MySQL Documentation page.

  2. Configure the server according to the MySQL documentation, including entering a root password.

  3. Start the MySQL server.

  4. Install the JRuby activerecord-jdbcmysql-adapter gem, if necessary.

    For example:

    gem install activerecord-jdbcmysql-adapter
  5. Modify the database.rake script for your Rails installation so it uses jdbcmysql rather than mysql.

    For example, if using the version of JRuby and Rails installed through GlassFish Server Update Center on Solaris or Linux, the database.rake file is located in:


    Change all instances of mysql with jdbcmysql, and then save the file.

ProcedureTo Create a Database-Backed Rails Application

  1. Create or select a directory for creating a database-backed Rails application, and change to that directory.

    In this example, a directory named /apps/jruby-apps is used.

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

    jruby -S rails books -d mysql

    This creates a books directory.

  3. Change to the books/config directory and open the config/database.yml file in a text editor.

    1. Replace all instances of adapter: mysql with adapter: jdbcmysql.

    2. Enter your MySQL root password under the development heading in the database.yml file.

  4. Save the file and change back to the books directory, if you are not already there.

  5. Create the database by running the following command:

    jruby -S rake 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.

  6. (Optional) If desired, verify that a database named books_development was successfully created.

    For example:

    mysql -u root -p -e 'show databases'
  7. Still in the books directory, 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.

  8. 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: migrating ====================================================
    -- create_table(:books)
       -> 0.1470s
       -> 0 rows
    ==  CreateBooks: migrated (0.1470s) ===========================================

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

    jruby —S rake db:reset
  9. (Optional) If desired, verify that two tables, named books and schema_migrations, were successfully created in the books_development database.

    For example:

    mysql -u root -p -e 'show tables from books_development'