Ruby on Rails
Oracle

Rails supports Oracle 8i, 9i, and 10g.

Be sure to check out HowtoConnectToOracle

The “database: ” keyword can take two forms, depending on whether you have a system-wide TNSNAMES.ORA file.
If this is the case, you can simply refer to the database by SID, like so:

development:
  adapter: oracle
  database: examplesid
  username: exampleuser
  password: examplepass

Otherwise, you can specify an Easy Connect connection string. This form can be used if you are using the Oracle 10gR2 (or more recent) full client or Instant Client libraries to connect to the Oracle Database:

development:
  adapter: oracle
  database: 192.168.0.50/examplesid
  username: exampleuser
  password: examplepass

For older versions (before 2006 March?), use “adapter: oci” and “host: 192.168.0.50/examplesid” instead of “adapter: oracle” and “database: 192.168.0.50/examplesid” respectively.

Usage notes:

Determining the SID

The SID can be found in the first field of your oratab file (often /etc/oratab on Linux). For example, the default SID for Oracle Express Edition is “XE”.

/etc/oratab

XE:/usr/lib/oracle/xe/app/oracle/product/10.2.0/server:N

database.yml

development:
  adapter: oracle
  database: 127.0.0.1/XE
  username: example
  password: examplepass

Getting the driver:

The OCI8 driver can be retrieved from http://rubyforge.org/projects/ruby-oci8/

MacOS X users may need some additional configuration.
Instructions for intel can be found here:
http://www.foliosus.com/2007/11/19/connecting-ruby-on-rails-to-oracle-on-an-intel-mac-in-leopard-mac-osx-105/

Where can I find help?

The best place to start is the OTN Discussion Forums at http://forums.oracle.com. There are users and developers there 24 hours a day answering questions. Be sure to use the search feature to see if your problem has already been addressed before posting!

You may also want to review the Oracle+RoR FAQ published on OTN at http://otn.oracle.com/pub/articles/saternos-ror-faq.html.

If your problem turns out to Rails-specific, try the RubyOnRails mailing list.
Resources

The above should print a single Emp with a default Oracle install with SID = examplesid.
(3) Test that database.yml is parsed correctly

ruby -ryaml -e "File.open('config/database.yml') { |f| puts YAML.load(f).inspect }" 

The above should print out the name value pairs. Beware of tabs (\t), as YAML forbids tabs. See the below output for an example that will cause a: ORA-12162: TNS:net service name is incorrectly specified

{”\tpassword”=>“tiger”, “development”=>{“adapter”=>“oracle”}, ”\tusername”=>“scott”, ”\tdatabase”=>“examplesid”}

Note the \t characters.
See Also

Unicode

If you are having trouble writing and reading utf8, check this out .It might save you.

Problems

I have tried all the connection strings on this page and I still get a TNS names error. I connect without problem using Perl or Python so I know my Oracle is setup correctly. Hints?

Category:DatabaseDrivers

Question
On p. 362 (or 359), Agile Web Development With Rails, 2nd Edition has a footnote that says Oracle 8 users cannot use preloading, presumably because left outer joins are required (9+ can). Now, I’ve used Oracle 8i, and it definitely has left outer joins, just with a different syntax. Does the current (1.2) adapter support preloading on 8i?—John Duncan

Answer (?) Rails 0.10.0 (24th February, 2005) added a OCI8-based Oracle adapter that has been verified to work with Oracle 8i through 10g

Rails versions after 0.13.1 have much-enhanced Oracle support, including separate sequences for each table, synonyms, etc.
See the relevant patch for more information:
http://dev.rubyonrails.org/ticket/1798
Rails configuration:

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

Question
Does the database have to be on localhost?

Example Connection
ActiveRecord::Base.establish_connection( :adapter => “oracle”, :database => “hostname:port/sid”, :username => “someone”, :password => “somepassword”
)

The atabase will look like one of these examples :database => “127.0.0.1:1521/XE” :database => “dbserver:1521/sales”