How to Configure Ruby on Rails with Oracle

How to configure Ruby on Rails with Oracle?

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

How to connect a rails app to a oracle database?

Look at "Ruby on Rails on Oracle: A Simple Tutorial or "How to configure Ruby on Rails with Oracle?".

How to fetch data from remote oracle database Ruby on rails

Install Oracle Instant Client and Ruby-OCI8

Install ActiveRecord oracle_enhanced adapter (version 1.3.1 is recommended)
gem install activerecord-oracle_enhanced-adapter

Replace the development section in the file config/database.yml with the following content and then save and close the file.

development:
adapter: oracle_enhanced
username: rubyhol
password: welcome
database: localhost/orcl

For more details follow this link: https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/rubyhol/instructions/rubyrails.htm

Oracle XE connection configuration for Ruby on Rails

Having ruby-oci8 installed is often the hard bit, so you're nearly there.

Activerecord needs an adapter to talk to any kind of database; for oracle you will want oracle enhanced.

So add this to your Gemfile then redo bundle install:

gem 'activerecord-oracle_enhanced-adapter'

Read the linked page for oracle-specific quirks. Your database.yml file should look like this:

database:
adapter: oracle_enhanced
database: //10.1.1.3:1521/XE
username: ruby
password: ruby

The "XE" in the database connection string is the SID. I think with oracle XE the SID is always "XE" but I don't have one handy and haven't used it since 10g.

You shouldn't need to add ruby-oci8 to your gemfile because the oracle adapter declares it as a dependency, but it doesn't hurt to add it.

Ruby on Rails Oracle DB connection

At first you need to install Oracle Instant Client - choose corresponding Linux 32-bit or 64-bit binaries. To be safe use older version 10.2.0.4 which I use in all my Ruby and Oracle projects. Install Basic, SDK and SQL*Plus packages.

Then include Oracle Instant Client installation directory in LD_LIBRARY_PATH environment variable so that Oracle Instant Client dynamic libraries would be in load path.

Then try to install ruby-oci8 gem.

Afterwards install activerecord-oracle_enhanced-adapter gem to be able to access Oracle database from ActiveRecord.

I have posted instructions how to install ruby-oci8 on Mac OS X - majority of this applies to Linux as well (DYLD_LIBRARY_PATH on Mac corresponds to LD_LIBRARY_PATH on Linux).

In addition I have created Sprinkle recipe for automated Oracle client installation on Linux - probably it is not the best way to start with this but in case you want to build automated server installation scripts then you can take a look on it.

Integrate an oracle Database View in my Ruby on Rails Application

Thanks everyone for your help!

It was satisfactory, as @JimEvans solution, to create a Model:

  class TankBank < ActiveRecord::Base
self.table_name = "GEVIS.TANK_BANK"

end


Related Topics



Leave a reply



Submit