In order to use oracle connection for your ruby on rails at windows, you have to do the following.

1. Install oracle instant client.

You can install any version of oracle, but oracle instant client is the easiest one to install.
Download it at http://download-llnw.oracle.com/otn/nt/instantclient/112010/instantclient-basic-win32-11.2.0.1.0.zip

After that, extract the files into C:/program files, or any directories that you like. Then add the instant client path to the environment variables ‘Path’.

2. Install Ruby Oracle library ruby-oci8

This library allows your rails application to talk to your oracle database server. It is a ruby interface for oracle to use OCI8 (Oracle Call Interface) API. The Oracle Call Interface (OCI) is an application programming interface (API) which allows developers to build applications using low-level C and C++ function calls to access an Oracle database server. Similarly, not only does OCI allow users to control all aspects of SQL statement execution, but it also fully supports the datatypes, calling conventions, syntax, and semantics of C and C++. (Extract from Oracle Wiki). Install using the following command:

gem install ruby-oci8

Do note that before installing ruby-oci8, make sure you have oracle instant client installed otherwise the OCI8 library will not be available on your machine.

3. Install activerecord-oracle_enhanced-adapter

Oracle “enhanced” ActiveRecord adapter contains useful additional methods for working with new and legacy Oracle databases from Rails. Refer to this gem’s github page for more details.

gem install activerecord-oracle_enhanced-adapter

Now you are free to use oracle connection in your rails applications. If you want to modify your database.yml to connect to oracle when in production, change the database.yml’s production entry to the following:

production:
adapter: oracle_enhanced
database: db_name
username: user_name
password: your_password

In case you still got problem, you can reference here for trouble shooting: https://github.com/rsim/oracle-enhanced/wiki/troubleshooting

8 comments on “Set up Oracle Connection for Ruby on Rails

  1. Fred on said:

    Thanks for this nice and clear HowTo.

    I still have an issue with my installation. The OCI layer is installed and works fine. The adpater seems to install correctly :
    C:railsDQEX>gem install activerecord-oracle_enhanced-adapter
    Fetching: activerecord-oracle_enhanced-adapter-1.3.2.gem (100%)
    Successfully installed activerecord-oracle_enhanced-adapter-1.3.2
    1 gem installed
    Installing ri documentation for activerecord-oracle_enhanced-adapter-1.3.2…
    Installing RDoc documentation for activerecord-oracle_enhanced-adapter-1.3.2…

    I get the RuntimeError : Please install the oracle_enhanced adapter: `gem install activerecord-oracle_enhanced-adapter` (no such file to load — active_record/connection_adapters/oracle_enhanced_adapter)

    The file is present in C:Rubylibrubygems1.9.1gemsactiverecord-oracle_enhanced-adapter-1.3.2libactive_recordconnection_adapters

    What’s wrong ?

    Thanks for your help.

    Kind regards,

    Fred

    • shanison on said:

      Hi, thanks for your comments. First, verify that your OCI is working. open your irb, and type
      require ‘rubygems’
      require ‘oci8′
      see if you get any error. if not, then try
      gem ‘activerecord-oracle_enhanced-adapter’
      see if you get any error. If any of the above steps have error, then you may wanna uninstall these gems(oci8, oracle enhanced adapter and instant client) then redo it again. Let me know if it helps.

      • Fred on said:

        Hi Shanison,
        Thanks for your help. Finally, only oracle enhanced adapter did not work, because it needs to be registered in the Gemfile of my project root (Rails 3.0).
        BR
        Fred

  2. rails_fan on said:

    hi shan,
    i have problem with configuring oracle with rails i have installed everything that mentioned here and set the path too ….but it shows activerecord oracle_enhanced adapter could not load ruby-oci8 …pl install ruby-oci8 give me a solution…or kindly explain me the above steps more clear…im new one to rails so only im asking u to explain more elaborate…im using rails3 and ruby 1.9

    • shanison on said:

      Did you put the ruby-oci8 in your Gemfile? For rails 3, it is using bundler, so it will only load the Gems specified in the Gemfile.

      gem “ruby-oci8″, “>=2.0.4″
      gem “activerecord-oracle_enhanced-adapter”, “>=1.3.0″

  3. rails_fan on said:

    hi shan again its me…:-)
    now i can able to connect oracle….but can’t load ruby-oci8 libraries
    while i try to run rake db:create in ror cmd prompt
    it shows rake aborted unable to load ruby-oci8 libraries.pl install ruby-oci8 gem
    kindly give me a solution

    • shanison on said:

      Ruby is using ruby-oci8 library to connect to oracle so I don’t quite understand what do you mean by you can connect oracle but can’t load ruby-oci8 library.

      You can test out if your ruby-oci8 library is working fine by running the following

      ruby -r oci8 -e “OCI8.new(‘username’, ‘pwd’).exec(‘select * from table’) do |r| puts r.join(‘,’); end”

      http://www.oracle.com/technetwork/articles/fernandez-rails-connections-101759.html

      • rails_fan on said:

        i can connect oracle means.. sql*plus (oracle) is working in my ruby and rails console window while i run rails db:console
        but when i run rake db:create it shows
        rake aborted! active record oracle enhanced adapter could not load ruby-oci8 library.so pl install ruby-oci8 gem.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.