Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Ruby on Rails with Oracle?

There's several pages on the web that discuss this, but most are out of date or inaccurate in some way.

What's the scoop?

like image 630
Mark Harrison Avatar asked Apr 19 '09 05:04

Mark Harrison


People also ask

How to connect to Oracle database using Ruby?

In order to connect to Oracle, you need to install the Ruby/Oracle Call Interface (OCI8) library, which is a database driver based on Ruby/DBI (Database Interface module). RubyDBI provides a database-independent interface for Ruby to talk to databases similar to JDBC or ODBC.

Does Oracle use Ruby on Rails?

At this point, we have used Ruby on Rails to create a web application on two existing tables (employees and jobs) that exist in the Oracle database.

What is Oracle Instant Client?

What is Instant Client? Instant Client is a repackaging of Oracle Database libraries, tools and header files usable to create and run applications that connect to a remote (or local) Oracle Database.


1 Answers

Build ruby, gem, and rails

as per http://rubyonrails.org/download:

build ruby
build gem
use gem to install rails

Get Oracle Instantclient

Download from https://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html

You need these two packages for your architecture.

instantclient-basic
instantclient-sdk

Unzip these files, and make this link

cd instantclient_10_2
# .dylib for mac, .so for linux
ln -s libclntsh.dylib.10.1 libclntsh.dylib

Build ruby-oci8

Note, JRuby users don't need ruby-oci8, but do need the Oracle JDBC jar, either ojdbc6.jar or ojdbc5.jar depending on whether you have Java 6 or Java 5.

Download from http://ruby-oci8.rubyforge.org/en/index.html and run

# DYLD for mac
export DYLD_LIBRARY_PATH=/path/to/instantclient_10_2
# LD for linux
export LD_LIBRARY_PATH=/path/to/instantclient_10_2
ruby setup.rb config
ruby setup.rb setup
ruby setup.rb install

Test with this line and your database connection string.

ruby -r oci8 -e "OCI8.new('scott/tiger@orcl').exec('select * from user_tables') do |r| puts r.join(','); end"

Install activerecord-oracle_enhanced-adapter

Note, not activrecord-oracle-adapter as many older pages mention.

gem install activerecord-oracle_enhanced-adapter

Do that sweet rails thing

rails railstest
cd railstest
# edit config/database.yml as below
ruby script/generate scaffold comic title:string issue:integer publisher:string
rake db:migrate
ruby script/server

Test in browser

<http://localhost:3000/comics>

config/database.yml

Use database if you have a TNS entry, otherwise use host. Note that you have three entries (devel, test, production) to update.

development:
    adapter: oracle_enhanced
    database: orcl           # format is tns-name entry
    host:  myorclhost/orcl   # format is hostname/instance-name
    username: scott
    password: tiger

References

  • http://emphaticsolutions.com/2008/05/22/connecting-to-oracle-from-ruby-on-rails.html
  • http://www.oracle.com/technology/pub/articles/saternos-ror-faq.html
  • http://drawohara.com/post/37166893/rails-unsucking-oci-oracle-on-rails-2-1
  • http://www.oracle.com/technology/pub/articles/haefel-oracle-ruby.html
like image 124
Mark Harrison Avatar answered Oct 09 '22 07:10

Mark Harrison