Monday, September 21, 2009

Oracle with JRuby using activerecord-jdbc-adapter

  • Download jruby from http://dist.codehaus.org/jruby/1.3.1/ and install it. For convenience you can set an environment variable JRUBY_HOME = C:\jruby-bin-1.3.1\ (/opt/jruby-1.3.1 for linux/OSX) and optionally you can add this environment variable to the PATH variable.

    set PATH=%PATH%;JRUBY_HOME (windows)
    export PATH=$PATH:JRUBY_HOME (linux or OSX)


  • install rails using jruby -S gem install -y rails (in my time the latest one was 2.3.3)
    If you didn't set the environment varilable in the path variable then you will have to execute following command:
    JRUBY_HOME\jruby -S gem install -y rails
    If you want to specify version then
    jruby -S gem install -y rails --version 2.3.3
  • install activerecord-jdbc-adapter (version - 0.8) using
    jruby -S gem install -y activerecord-jdbc-adapter --version 0.8 or
    JRUBY_HOME\jruby -S gem install -y activerecord-jdbc-adapter --version 0.8

    At first I installed the newer version 0.9.1 but with that version I was getting following error for timestamp type column: "undefined method 'new_date' for JdbcSpec::Oracle::Column:Module". I have a plan to solve this problem later
  • Now its time to test activerecord, activerecord-jdbc-adapter, jdbc-driver from jirb. To test we need couple of tables in the database. Lets say we have two tables:
    - authors
    - articles [an author can have multiple articles]
  • Now copy the oracle-jdbc-driver jar file (ojdbc14-10.2.0.1.0.jar) in the JRUBY_HOME\lib folder then run 'jirb'. To run "jirb" in command prompt go to JRUBY_HOME\bin and type 'jirb'. Now in the 'irb' console type followings:

    require 'rubygems'
    require 'active_record'
    require 'active_record/connection_adapters/oracle_adapter'

    class Author < adapter =""> 'jdbc',
    :driver => 'oracle.jdbc.driver.OracleDriver',
    :url => 'jdbc:oracle:thin:@localhost:1521:XE',
    :username => 'username',
    :password => 'password')

    Author.all()
Here you go !! You are done !!

Some useful links:

No comments: