Miles to go …

October 7, 2009

TOTD #111: Rails Scaffold for a pre-existing table using Oracle and GlassFish

Filed under: frameworks, glassfish, rails — Tags: — arungupta @ 5:01 am

TOTD #110 explained how to create a brand new Rails application using Oracle database and run it using GlassFish v Gem. This Tip Of The Day explains how to create a scaffold for a sample schema that ships with Oracle database. Even though Rails Scaffold are good for, well, scaffolding but they do get you started easily. This blog will use the sample HR schema that comes along with Oracle database.

Lets get started!

  1. Copy the reverse_scaffold script in the "script" directory of your application created in TOTD #110. This script generates Model and Forms from a pre-existing database table. More details about this script are here.
  2. Edit "config/database.yml" and change the "development" section to:

    development:
    adapter: oracle_enhanced
    host: localhost
    database: orcl
    username: hr
    password: hr

    The changes are highlighted in bold, only the username and password values are changed to reflect the default values used with the sample database.

  3. Generate the models and forms for "departments" table as:

    ~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby script/reverse_scaffold departments department
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    
     exists app/models/
     exists app/controllers/
     exists app/helpers/
     create app/views/departments
     exists app/views/layouts/
     exists test/functional/
     exists test/unit/
     create test/unit/helpers/
     exists public/stylesheets/
     create app/views/departments/index.html.erb
     create app/views/departments/show.html.erb
     create app/views/departments/new.html.erb
     create app/views/departments/edit.html.erb
     create app/views/layouts/departments.html.erb
     create public/stylesheets/scaffold.css
     create app/controllers/departments_controller.rb
     create test/functional/departments_controller_test.rb
     create app/helpers/departments_helper.rb
     create test/unit/helpers/departments_helper_test.rb
     route map.resources :departments
     dependency model
     exists app/models/
     exists test/unit/
     exists test/fixtures/
     create app/models/department.rb
     create test/unit/department_test.rb
     create test/fixtures/departments.yml
    
  4. Edit "app/models/department.rb" and specify the primary key to "department_id" column by adding:

    set_primary_key "department_id"
    

  5. Run the application as:

    ~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S glassfish -l
    Starting GlassFish server at: 129.145.133.197:3000 in development environment...
    Writing log messages to: /Users/arungupta/samples/v3/rails/oracle/bookstore/log/development.log.
    Press Ctrl+C to stop.
    Oct 6, 2009 2:14:19 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 3000
    
    . . .
    

    The application is now accessible at "http://localhost:3000/departments" and looks like:

  6. Similarly, create the model and forms for "employees" table as:

    ~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby script/reverse_scaffold employees employee
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    
     exists app/models/
     exists app/controllers/
     exists app/helpers/
     create app/views/employees
     exists app/views/layouts/
     exists test/functional/
     exists test/unit/
     exists test/unit/helpers/
     exists public/stylesheets/
     create app/views/employees/index.html.erb
     create app/views/employees/show.html.erb
     create app/views/employees/new.html.erb
     create app/views/employees/edit.html.erb
     create app/views/layouts/employees.html.erb
     identical public/stylesheets/scaffold.css
     create app/controllers/employees_controller.rb
     create test/functional/employees_controller_test.rb
     create app/helpers/employees_helper.rb
     create test/unit/helpers/employees_helper_test.rb
     route map.resources :employees
     dependency model
     exists app/models/
     exists test/unit/
     exists test/fixtures/
     create app/models/employee.rb
     create test/unit/employee_test.rb
     create test/fixtures/employees.yml
    

    Specify the primary key to "employee_id" by adding the following to "app/models/employee.rb" as:

    set_primary_key "employee_id"
    

    The scaffolded table is now available at "http://localhost:3000/employees" and looks like:

So we created a simple Rails CRUD application accessing information from a pre-existing table in the Oracle database server.

Thanks to @mediachk for all the help!

A complete archive of all the TOTDs is available here. The complete list of Rails blog entries are available here.

This and other similar applications will be demonstrated at the upcoming Oracle Open World.

Technorati: totd oracle database glassfish v3 jruby rails oow

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #110: JRuby on Rails application using Oracle on GlassFish
  2. TOTD #28: Getting Started with Rails 2.0 Scaffold
  3. TOTD #37: SQLite3 with Ruby-on-Rails on GlassFish Gem
  4. Getting Started with Edge Rails (2.2) using JRuby and GlassFish
  5. TOTD #53: Scaffold in Merb using JRuby/GlassFish

2 Comments »

  1. Thanks for recommending oracle_enhanced adapter :)

    With oracle_enhanced adapter you can make your database.yml simpler:

    development:
    adapter: oracle_enhanced
    host: localhost
    database: orcl
    username: hr
    password: hr

    Raimonds

    Comment by Raimonds — October 9, 2009 @ 4:34 am

  2. Thanks a lot Raimonds!

    Updated the post to incorporate the simplified change.

    Comment by arungupta — October 9, 2009 @ 9:06 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress