简体   繁体   中英

Oracle XE connection configuration for Ruby on Rails

I am fairly new to Ruby on Rails and am currently having a problem with configuring config/database.yml to connect Oracle XE 11g R2. I need your help!

Here is my setup:
VirtualBox 4.3.6
Ubuntu 64 v12.04.3

$ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]

$ rails -v
Rails 4.0.2

$ gem -v
2.2.2

$ gem install ruby-oci8
Building native extensions.  This could take a while...
Successfully installed ruby-oci8-2.1.7
Parsing documentation for ruby-oci8-2.1.7
Done installing documentation for ruby-oci8 after 4 seconds
1 gem installed

# Oracle XE 11gR2 installed in a different VM (Host: 10.1.1.3)
#
$ ruby -r oci8 -e 'OCI8.new("ruby/ruby@10.1.1.3").exec("select * from session_privs") do |r| puts r.join(","); end'
CREATE SESSION
UNLIMITED TABLESPACE
CREATE TABLE
CREATE CLUSTER
CREATE SEQUENCE
CREATE PROCEDURE
CREATE TRIGGER
CREATE TYPE
CREATE OPERATOR
CREATE INDEXTYPE

Note that I can verify connection using the command above. However, I cannot create a new model using the "rails generate model Post title:string text:text" command - I get a long list of errors which complain that my "adapter" is not valid.

Questions:

Q1. What do I have to put in the Gemfile?

Q2. Should I see ruby-oci8 gem in the output of the command "bundle install" - currently I can see "Using sqlite3 (1.3.9) and there also 2 entries in the Gemfile as follows:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

Q3. Has anyone successfully configured database.yml to work with Oracle XE? Please share it with me.

Thanks in advance for your help,
Roobie

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM