Ruby on Rails
HowtoConnectToOracle (Version #79)

Assuming you have

Your config/database.yml will look something like:

development:
   adapter:oci
   database: orcl
   host:
   username: username
   password: password
  • It works even if your database is on a remote host (configured in tnsnames, requires a listener is running on remote host).
  • I had to set LD_LIBRARY_PATH=/Path/to/$ORACLE_HOME/lib to make the oci driver work on Linux.

At least in AR 1.10.1, the OCI adapter doesn’t use database, it uses host, so that should be:

development:
  adapter: oci
  database:
  host: orcl
  username: username
  password: password

If you don’t have a tnsnames.ora configured for your environment, and you are using the 10g instantclient install, you can specify a connect string for the host parameter. Example:

development:
  adapter: oci
  host: //server.example.com:port/instance_name
  username: username
  password: password

Note: Make sure you Install Active Record. gem install activerecord
Note: Please make sure that the ruby/OCI8 driver(http://rubyforge.org/projects/ruby-oci8/) is installed or else an AdapterNotFound exception will be thrown when starting the web server

THIS IS THE PLACE TO GO FOR MAC >
http://creativi.st/blog/articles/2005/06/25/rails-oracle-client-on-mac-os-x

Newer instructions for Intel Macs Better new instructions for Intel Macs (using the new Intel Mac Oracle InstantClient)

just remember to do this also, so your “ruby.h” (ruby header file) is found for the ‘make’ command.

cd /usr/lib/ruby/1.8/powerpc-darwin8.0
sudo ln -s ../universal-darwin8.0/* ./

found here:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/76928193a89bd420/5b7d897c4316a6d0

- Grant Aaron

Another Note: I had to try several configurations before this worked for me. My Configuration:

- Rails 1.0.0
- activerecord (1.13.2)
- Oracle 90 client with configured TNSNAMES.ora
- Default MSQL DB in ‘database.yml’
- ruby/OCI8 driver for windows
- Oracle Database connection defined in my object model:
class Stream < ActiveRecord::Base
establish_connection({
:adapter => “oci”,
:host => “SID.COMPANY.NET”,
:username => “user”,
:password => “pass”
})
set_table_name “mytable”
end

My specific issue was that the entry name in TNSNAMES.ora is not the same as the service name of that entry. The host must contain the entry name (which happens to be ‘SID.COMPANY.NET’) in TNSnames and not the service name (ex: ‘sid’).

To use Oracle with FastCGI, see HowToUseOracleWithFastCGI

Your issue had nothing to do with using establish_connection over database.yml. My bet would be that if you pulled your establish connection back out into database.yml it will work fine now. Rails uses database.yml to load a variable called RAILS_ENV which is (by default) read from in establish_connection. All you did (too much work) was overwrite establish_connection to do this manually.

Using ‘oracle’ rather than ‘oci’ as an adapter

(Full article here)

If you want to use the ‘new’ oracle adapter rather than oci and you want to use host/SID style of database server reference you have to use ‘database’ rather than ‘host’, see [PATCH] Rename oracle_adapter, keep up w/ the Joneses for more details. – AndrewBeacock

Rails 2 and Oracle

database.yml


development:
adapter: oracle
database: [your database as it appears on TNSNAMES.ora]
username: [your username]
password: [your password]

The oracle adapter does not exist on Rails 2 by default so:

  1. Check out the oracle adapter
    svn co http://svn.rubyonrails.org/rails/adapters/oracle/lib/active_record/connection_adapters/
  2. And move oracle_adapter.rb to your ActiveRecord adapters path
    [your ruby path]/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters
  3. Try again, it should work

Daniel

Assuming you have

Your config/database.yml will look something like:

development:
   adapter:oci
   database: orcl
   host:
   username: username
   password: password
  • It works even if your database is on a remote host (configured in tnsnames, requires a listener is running on remote host).
  • I had to set LD_LIBRARY_PATH=/Path/to/$ORACLE_HOME/lib to make the oci driver work on Linux.

At least in AR 1.10.1, the OCI adapter doesn’t use database, it uses host, so that should be:

development:
  adapter: oci
  database:
  host: orcl
  username: username
  password: password

If you don’t have a tnsnames.ora configured for your environment, and you are using the 10g instantclient install, you can specify a connect string for the host parameter. Example:

development:
  adapter: oci
  host: //server.example.com:port/instance_name
  username: username
  password: password

Note: Make sure you Install Active Record. gem install activerecord
Note: Please make sure that the ruby/OCI8 driver(http://rubyforge.org/projects/ruby-oci8/) is installed or else an AdapterNotFound exception will be thrown when starting the web server

THIS IS THE PLACE TO GO FOR MAC >
http://creativi.st/blog/articles/2005/06/25/rails-oracle-client-on-mac-os-x

Newer instructions for Intel Macs Better new instructions for Intel Macs (using the new Intel Mac Oracle InstantClient)

just remember to do this also, so your “ruby.h” (ruby header file) is found for the ‘make’ command.

cd /usr/lib/ruby/1.8/powerpc-darwin8.0
sudo ln -s ../universal-darwin8.0/* ./

found here:
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/76928193a89bd420/5b7d897c4316a6d0

- Grant Aaron

Another Note: I had to try several configurations before this worked for me. My Configuration:

- Rails 1.0.0
- activerecord (1.13.2)
- Oracle 90 client with configured TNSNAMES.ora
- Default MSQL DB in ‘database.yml’
- ruby/OCI8 driver for windows
- Oracle Database connection defined in my object model:
class Stream < ActiveRecord::Base
establish_connection({
:adapter => “oci”,
:host => “SID.COMPANY.NET”,
:username => “user”,
:password => “pass”
})
set_table_name “mytable”
end

My specific issue was that the entry name in TNSNAMES.ora is not the same as the service name of that entry. The host must contain the entry name (which happens to be ‘SID.COMPANY.NET’) in TNSnames and not the service name (ex: ‘sid’).

To use Oracle with FastCGI, see HowToUseOracleWithFastCGI

Your issue had nothing to do with using establish_connection over database.yml. My bet would be that if you pulled your establish connection back out into database.yml it will work fine now. Rails uses database.yml to load a variable called RAILS_ENV which is (by default) read from in establish_connection. All you did (too much work) was overwrite establish_connection to do this manually.

Using ‘oracle’ rather than ‘oci’ as an adapter

(Full article here)

If you want to use the ‘new’ oracle adapter rather than oci and you want to use host/SID style of database server reference you have to use ‘database’ rather than ‘host’, see [PATCH] Rename oracle_adapter, keep up w/ the Joneses for more details. – AndrewBeacock

Rails 2 and Oracle

database.yml


development:
adapter: oracle
database: [your database as it appears on TNSNAMES.ora]
username: [your username]
password: [your password]

The oracle adapter does not exist on Rails 2 by default so:

  1. Check out the oracle adapter
    svn co http://svn.rubyonrails.org/rails/adapters/oracle/lib/active_record/connection_adapters/
  2. And move oracle_adapter.rb to your ActiveRecord adapters path
    [your ruby path]/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters
  3. Try again, it should work

Daniel