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:
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.
- 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:
set_primary_key "department_id"
- 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:
- 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
GlassFish v3 is the Reference Implementation for Java EE 6. Following the "extensibility" principle of Java EE 6, it also allows Ruby-on-Rails, Groovy and Grails and Python/Django applications to be seamlessly deployed as well, without any additional packaging. This blog has published multiple entries on deploying a Rails application on GlassFish as given below:
- TOTD #105: Monitor Rails application using JavaScript
- TOTD #104: Redmine, Typo, Substruct on GlassFish v3
- TOTD #84: Apache + mod_proxy_balancer to load balance Rails applications on GlassFish
- TOTD #81: nginx to load balance Rails applications on GlassFish Gem
- TOTD #73: Deploying Rails application as WAR on GlassFish v2.1
- TOTD #72: Deploying Rails application on GlassFish v3
- TOTD #70: Deploying Rails application on GlassFish Gem
All the existing applications have used JavaDB, SQLite3, or MySQL as the database so far. In the process of getting ready for the upcoming Oracle Open World 2009, this Tip Of The Day will show how to use an Oracle database with a JRuby-on-Rails application deployed on GlassFish v3.
Lets get started!
- Install Oracle database as explained in TOTD #106.
- Configure JRuby/Rails in GlassFish v3 using one of the mechanisms explained in TOTD #104. Alternatively you can also install the GlassFish gem as:
>./bin/jruby -S gem install glassfish
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed rack-1.0.0
Successfully installed glassfish-0.9.5-universal-java
2 gems installed
Installing ri documentation for rack-1.0.0...
Installing ri documentation for glassfish-0.9.5-universal-java...
Installing RDoc documentation for rack-1.0.0...
Installing RDoc documentation for glassfish-0.9.5-universal-java...
This blog will use GlassFish Gem for running the application described below.
- Create a new database user and grant rights using SQL*Plus as shown:
Macintosh-187:~ oracle$ sqlplus "/ as sysdba"
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Oct 1 12:32:33 2009
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Release 10.2.0.4.0 - Production
SQL> CREATE USER glassfish IDENTIFIED BY glassfish DEFAULT tablespace users TEMPORARY tablespace temp;
User created.
SQL> GRANT CONNECT TO glassfish IDENTIFIED BY glassfish;
Grant succeeded.
SQL> GRANT UNLIMITED TABLESPACE TO glassfish;
Grant succeeded.
SQL> GRANT CREATE TABLE TO glassfish;
Grant succeeded.
SQL> GRANT CREATE SEQUENCE TO glassfish;
Grant succeeded.
SQL> exit
Disconnected from Oracle Database 10g Release 10.2.0.4.0 - Production
The user name and password are chosen as "glassfish" for simplicity. This is not a recommended setting for production usage though.
- Copy Oracle JDBC drivers (odjc6.jar) in JRUBY_HOME/lib directory.
- Create a simple Rails application
- Make sure the following gems are pre-installed:
rails (2.3.4)
activerecord-jdbc-adapter (0.9.2)
glassfish (0.9.5)
If not, then install them as:
jruby -S gem install rails activercord-jdbc-adapter glassfish
- Create a simple Rails application as:
jruby -S rails bookstore -d oracle
- Using the normal "jdbc" adapter will give the following error later:
ActionView::TemplateError (book_url failed to generate from {:controller=>"books", :action=>"show", :id=>#<Book id: #<BigDecimal:3feef1eb,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}, expected: {:controller=>"books", :action=>"show"}, diff: {:id=>#<Book id: #<BigDecimal:459bdb65,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}) on line #13 of app/views/books/index.html.erb:
As evident, the "id" column is returned as BigDecimal where as it should be integer. Fortunately the fix is simple, install the "oracle_enhanced_adapter" (docs) as:
bookstore >~/tools/jruby/bin/jruby -S gem install activerecord-oracle_enhanced-adapter
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed activerecord-oracle_enhanced-adapter-1.2.2
1 gem installed
Installing ri documentation for activerecord-oracle_enhanced-adapter-1.2.2...
Installing RDoc documentation for activerecord-oracle_enhanced-adapter-1.2.2...
Using this "enhanced adapter" is highly recommended for connecting with Oracle databases from Rails applications.
- Edit "config/database.yml" and change the "development" section to:
development:
adapter: oracle_enhanced
host: localhost
database: orcl
username: glassfish
password: glassfish
Notice, the username and password values are the same as chosen in the SQL statements above.
- Generate a scaffold as:
bookstore >~/tools/jruby/bin/jruby script/generate scaffold book title:string author:string
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/books
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/books/index.html.erb
create app/views/books/show.html.erb
create app/views/books/new.html.erb
create app/views/books/edit.html.erb
create app/views/layouts/books.html.erb
create public/stylesheets/scaffold.css
create app/controllers/books_controller.rb
create test/functional/books_controller_test.rb
create app/helpers/books_helper.rb
create test/unit/helpers/books_helper_test.rb
route map.resources :books
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/book.rb
create test/unit/book_test.rb
create test/fixtures/books.yml
create db/migrate
create db/migrate/20091005233152_create_books.rb
- Prepare your application for JDBC as:
bookstore >~/tools/jruby/bin/jruby script/generate jdbc
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
exists config/initializers
create config/initializers/jdbc.rb
exists lib/tasks
create lib/tasks/jdbc.rake
- Migrate the database as:
~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S rake db:migrate
(in /Users/arungupta/samples/v3/rails/oracle/bookstore)
== CreateBooks: migrating ====================================================
-- create_table(:books)
-> 0.0740s
-> 0 rows
== CreateBooks: migrated (0.0750s) ===========================================
- Lets 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 9:45:51 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 3000
. . .
he application is now accessible at "http://localhost:3000/books" and looks like:
Click on "New Book" and enter the values as shown:
Click on "Create" to see the output as:
Click on "Back" to see the main page as:
After adding another book, this page looks like:
And another book …
So we created a brand new JRuby/Rails application and ran it using GlassFish and Oracle backend. A subsequent blog entry will show how to create a similar application using an existing database.
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