How to Use Rails to connect to an Oracle database

Source: Internet
Author: User

Preparations:

1) install ActiveRecord Oracle adapter

Sudo gem install activerecord-oracle-adapter-source http://gems.rubyonrails.org

2) install the Oracle ClientHttp://www.oracle.com/technology/tech/oci/instantclient/index.html

An error may occur during the installation process. Generally, a prompt is displayed, and the following steps may be performed:

2.1) sudo ln-s libclntsh. dylib.10.1 libclntsh. dylib

2.2) set the environment variable. You can add it in. profile for Mac.

 
 
  1. export DYLD_LIBRARY_PATH="/usr/local/oracle/instantclient_10_2" 

3) install the ruby oracle driver and modify the driver according to your system environment.

Sudo env DYLD_LIBRARY_PATH = $ DYLD_LIBRARY_PATH ARCHFLAGS = "-arch x86_64" gem install ruby-oci8

Installation references: http://blog.rayapps.com/2009/09/06/how-to-setup-ruby-and-oracle-instant-client-on-snow-leopard/

The project requires the Rails application to connect to mysql and Oracle DB at the same time, so you only need to modify the configuration in the config/database. yml file:

The following configuration generally uses the 'development' configuration by default and connects to the sqlite3 database.

 
 
  1. development:  
  2.    adapter: sqlite3  
  3.    database: db/development.sqlite3   
  4.    pool: 5   
  5.    timeout: 5000  
  6.   
  7. iisp:   
  8.    adapter: oracle   
  9.    encoding: utf8   
  10.    database: 192.168.68.22/iisp.xjgz   
  11.    username: iisp916   
  12.    password: iisp916  

In iisp/database, the IP address is followed by the database Sid, not the database Name

Add the following code to the model to connect to oracle:

 
 
  1. Establish_connection 'iisp'
  2. Self. table_name = 't_ capacity'
  3. Self. sequence_name ='s _ capacity '# primary key generation

Set the code for Rails to connect to Oracle:

Add the following code to config/environment. rb:

 
 
  1. ENV['NLS_LANG'] = 'AMERICAN_AMERICA.UTF8' 

Reference: http://wiki.rubyonrails.org/database-support/oracle


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.