tutorial provided by Oracle.
Part 2: My-first-app with Oracle
This guide assumes you are either:
- connecting to an Oracle Database installed on your local machine.
- connecting to a remote database using Oracle Instant Client installed on your local machine.
Install Oracle Database Driver
Download the driver and install with:
You can test your connection to the database with the following one-liner:
ruby -r oci8 -e "OCI8.new('username', 'password', '127.0.0.1:1521/orcl').exec('SELECT * FROM test_table') do |r| puts r.join('|'); end"
Of course, make sure the schema & table that you are testing exists!
Create a User
Using SQL*Plus, create a user with DBA privileges that you can use for this application.
ruby DEFAULT TABLESPACE users TEMPORARY TABLESPACE temp;
EXIT dba TO ruby IDENTIFIED BY ruby;
Create a Table
id NUMBER(10) NOT NULL,
PRIMARY KEY (id)
Alternatively use this SQL file to create the application table, COMICS.
sqlplus ruby/ruby@rails @comics.sql
Note, in this case the database SID is ‘rails’. The default SID for Oracle Enterprise Edition 10g is ‘orcl’, for Express Edition it’s ‘XE’
Create a dinky Application
Your application skeleton:
Edit ‘comics_catalog/config/databases.yml’ – rails needs to know your login & password info.
Within your project directory, there is a directory called config and in it is a file named database.yml. You need to edit database.yml using your favorite text editor. Initially, the file will look like this:
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
Change the development properties as follows:
host: takes a connect string of the form <ip_address>:<port>/<database> for the machine that your Oracle instance resides on.
Use the magical scaffold command to build a web based CRUD interface to the table we created above...
ruby script/generate scaffold Comic
Now you can access your Rails Comic Catalog application on your own development machine, using your favorite Web browser. Just access the following URL: http://localhost:3000/comics/list
Phew! Done. Last step is Part 3: Pen of pesky Mongrels...