Accessing Oracle10g in Ruby On Rails

If you want to build Ruby on Rails application to connect to Oracle10g database, you have to install Ruby/OCI8 from Rubyforge. After downloading and extracting the file, please read README file. Before compiling, I have to check the Oracle connection and prepare some environment variables :

ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME
ORACLE_SID=budsus; export ORACLE_SID
ORACLE_TERM=xterm; export ORACLE_TERM
PATH=$ORACLE_HOME/bin:$PATH; export PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib:${LD_LIBRARY_PATH}

Here is the sqlplus command to check the Oracle database connection from my session :

sqlplus hr/hr@localhost:1521/budsus

If you get the SQL> prompt from SQL*Plus environment, you have successfull to connect to Oracle database.

Here are some steps to create a simple RoR application to access HR.EMPLOYEES table :

1. rails hr

2. I read a helpful documentation from http://wiki.rubyonrails.org/rails/pages/Oracle to help me to understand about the database configuration. Here is my database.yml configuration :

development:
  adapter: oci
  host: (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=localhost)(Port=1521))(CONNECT_DATA=(SID=budsus)))
  username: hr
  password: hr

(note: if you are using OracleXE, you must use XE as SID)

3. ruby script/generate model Employee

4. ruby script/generate scaffold Employee

5. ruby script/server

From my browser, I can access http://localhost:3000/employees/list. I hope this article can help you. Thanks