読者です 読者をやめる 読者になる 読者になる

普通のRubyスクリプトからOracle 11g XEにActiveRecordで繋ぎに行く

必要なgemをインストール

PS:Desktop> gem install activerecord-oracle_enhanced-adapter
Fetching: activerecord-oracle_enhanced-adapter-1.4.2.gem (100%)
Successfully installed activerecord-oracle_enhanced-adapter-1.4.2
1 gem installed
Installing ri documentation for activerecord-oracle_enhanced-adapter-1.4.2...
unable to convert "\xE2" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to Windows-31J for lib/active_record/connection
_adapters/oracle_enhanced_schema_statements.rb, skipping
Installing RDoc documentation for activerecord-oracle_enhanced-adapter-1.4.2...
unable to convert "\xE2" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to Windows-31J for lib/active_record/connection
_adapters/oracle_enhanced_schema_statements.rb, skipping

PS:Desktop> gem install ruby-oci8
Fetching: ruby-oci8-2.1.5-x86-mingw32.gem (100%)
Successfully installed ruby-oci8-2.1.5-x86-mingw32
1 gem installed
Installing ri documentation for ruby-oci8-2.1.5-x86-mingw32...
Installing RDoc documentation for ruby-oci8-2.1.5-x86-mingw32...

じゃー繋いじゃうぞ

test.rb

# encoding: utf-8

require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
  adapter: 'oracle_enhanced',
  username: 'hr',
  password: 'hr',
  database: 'xe'
  )

class AllObject < ActiveRecord::Base; end

AllObject.where(owner: 'HR').each do |r|
  p r.object_name # => "REGIONS"
                  #    "COUNTRIES"
                  #    "LOCATIONS"
                  #    "DEPARTMENTS"
                  #    "JOBS"
                  #    "EMPLOYEES"
                  #    "JOB_HISTORY"
end

(^ω^)にこっ

参考