Rails on Oracle: Part 2 - Setting up Rails to connect to an Oracle database
Anton Jenkins | March 26, 2009

Ok, so this post is a bit earlier than advertised, but it’s better than being late! Following up from Rails on Oracle: Part 1 – Installing the oracle instant client on Mac OS X, now we are going to get rails to use our freshly installed oracle client…
Step 1: Download ruby-oci8
Download the latest tarball from the ruby-oci8 rubyforge page to your ~/Downloads directory. At the time of writing the latest is version 1.0.5.
Step 2: Make and install ruby-oci8
If you’ve followed the instructions from part 1 then you should have your oracle instant client installed in /opt/oracle/instantclient. If you’ve not installed the instant client yet then scoot off to part 1 and perform those steps before going any further. If you’ve installed it somewhere different then you will need to amend the following commands where appropriate:
1 2 3 4 5 6 7 |
cd ~/Downloads tar xvzf ruby-oci8-1.0.5.tar.gz cd ruby-oci8-1.0.5 ruby setup.rb config -- --with-instant-client=/opt/oracle/instantclient make sudo make install |
Step 3: Install the oracle_enhanced adapter
1 |
sudo gem install activerecord-oracle_enhanced-adapter |
Step 4: Configure you rails project to use an oracle database
Lets say with have the same oracle database as we had in part 1 which looked like so:
- Host : myserver
- domain : mydomain.com
- Port : 1521
- SID : mydatabase
- Service : mydatabase.mydomain.com
We’d express this in our database.yml file with the following entry:
1 2 3 4 5 |
development: adapter: oracle_enhanced database: myserver:1521/mydatabase.mydomain.com username: myusername password: mypassword |
Step 5: Profit!
Now (fingers crossed!) you should be ready. It’s been tricky for me to test this thoroughly because I don’t have access to a clean mac to start from scratch with. My mac is already set up and ready to go so it’s possible I could have missed something. If you have problems then leave a comment and I’ll try my best to sort you out and and update the instructions.
Looking for part 1?
Part 1 : Instructions on how to install the Oracle instant client on OS X and test it with sqlplus.












Anton,
This is a great tutorial and saved me from having to write up one of my own :)
Just like you did in Part 1 I'd suggest you include some validation steps to make sure everything is setup properly.
To validate Ruby can talk to Oracle (i.e. the OCI8 library is properly configured) run the following command line and you should see the systemdate as output
> ruby -r oci8 -e "OCI8.new('username', 'password', 'server.company.com:1521/sid.world').exec('SELECT sysdate FROM dual') { |r| puts r.join(' | '); }" 2009/04/10 14:44:51> script/console >> ActiveRecord::Base.connection => #<ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter:0x98e2e1c @connection=#<ActiveRecord::ConnectionAdapters::OracleEnhancedOCIConnection:0x98e2e08 @raw_connection=#<OCI8:0x98e2d7c @privilege=nil, @prefetch_rows=100, @svc=#<OCISvcCtx:0x98e2d18>, @ctx=[32, #<Mutex:0x98e2d2c>, nil, 65535]>>, @query_cache_enabled=false, @logger=#<ActiveSupport::BufferedLogger:0x995f098 @buffer={}, @guard=#<Mutex:0x995ee54>, @auto_flushing=1, @log=#<File:/myapp/log/devutility.log>, @level=0>, @last_verification=0, @runtime=0>Thanks for the tips Alex! I'll slip those validations into the post when I get some spare time.
Installing ruby-oci8-1.0.5 on Mac OS X I had to set the RC_ARCHS variable as suggested in the make help.
RC_ARCHS=i386
export RC_ARCHS
I followed this and got everything working with WebBrick. When I moved to Apache and Passenger it no longer works. The db connect happens and disconnects right away. I get the following messages:
Apache:
500 Internal Server Error
development.log:
Processing SessionsController#new (for ::1 at 2009-05-14 21:55:22) [GET]
Session ID: bea98f4422ef16002486f24d77312015
Rendering sessions/new
Completed in 15ms (View: 9, DB: 92) | 200 OK [http://localhost/login]
Processing SessionsController#new (for ::1 at 2009-05-14 21:55:22) [GET]
Session ID: bea98f4422ef16002486f24d77312015
OCIError (Error while trying to retrieve text for error ORA-03114):
svcctx.c:114:in oci8lib.so
/opt/local/lib/ruby/gems/1.8/gems/ruby-oci8-1.0.5/lib/oci8.rb:283:in `commit'
/opt/local/lib/ruby/gems/1.8/gems/ruby-oci8-1.0.5/lib/oci8.rb:142:in `do_ocicall'
/opt/local/lib/ruby/gems/1.8/gems/ruby-oci8-1.0.5/lib/oci8.rb:283:in `commit'
/opt/local/lib/ruby/gems/1.8/gems/ruby-oci8-1.0.5/lib/oci8.rb:301:in `autocommit='
Is there something special about passenger and apache that I should be aware of?
@razer : Are you still trying to run this on your development box or are you deploying? Because if this is tripping up in a deployment environment it's probably because the user which passenger will assume to run the app wont have the necessary environment variables set. For deployments I would set the NLS_LANG, ORACLE_HOME and LD_LIBRARY variables somewhere global where they will get set when the machine boots (/etc/profile.local /etc/bashrc or somwhere along those lines. It depends on your OS).
My guess as to the problem you're having above is Oracle can't find the files it needs. When Oracle has an error it looks for the error messages it should use in ORACLE_HOME. Messages for various languages will be stored in there.
Check to make sure your ORACLE_HOME variable is set. It looks like your LD_LIBRARY_PATH is OK as it's finding the shared libraries.