TOTD #111: Rails Scaffold for a pre-existing table using Oracle and GlassFish
By arungupta on Oct 07, 2009
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!
- 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.
- Edit "config/database.yml" and change the "development" section to:
The changes are highlighted in bold, only the username and password values are changed to reflect the default values used with the sample database.
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
Edit "app/models/department.rb" and specify the primary key to "department_id" column by adding:
Run the application as:
~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S glassfish -l Starting GlassFish server at: 188.8.131.52: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:
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:
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!
This and other similar applications will be demonstrated at the upcoming Oracle Open World.