Thursday, June 12, 2008

Getting Oracle to work with Rails 2

I'm working on a rails application that needs to connect to an Oracle database and we are using Rails 2.1. I tried to install ruby-oci8 and it was giving me errors, so here is the low down on getting it to work.

1) First you need to have the oracle client on your machine. I wanted to test things off site so I installed the 10g Express Edition (which is free) on my Ubuntu box and it worked great and included the client too. That meant that I didn't have to install the InstantClient or the other client, so I don't know how to do that yet. This wiki page gave me everything I needed to install the 10g xe on ubuntu.

2) You need to install the ruby-oci8 oracle driver bindings. On windows there is a binary, but on UNIX/Linux you have to compile it from source. Some people had troubles, but again, the 10g XE put all the headers and other files right where I needed them. Here is a great run down of how to compile the driver on Ubuntu with all its potential pitfalls.

3) Then you need the activerecord oracle adapter which doesn't come with Rails 2 by default. The Ruby on Rails wiki said to download this file and put it at "/lib/active_record/connection_adapters" in your active_record gem or rails installation.

4) Finally you need to create a user on the Oracle server and put the database "name" in your database.yml file. Here is the string I used for the 10g XE server: "database:". The service part after the host and port needs to come from the tnsnames.ora file on the client machine. You don't need a "host:" entry. This is because the host is in the database line and the username defines your tablespace so you don't have a database name like you do in mysql.

After all that try and run your migrations and it should work.

No comments: