JRuby で ActiveRecord 3 をちょこっと試す

JRuby で ActiveRecord を試してみたいと思いました。
JRuby は RVM で 1.5.6 がインストールされています。

$ echo "rvm jruby" >> .rvmrc

こんな感じで .rvmrc を使用して JRuby 環境になっているものとします。

まずは ActiveRecord のインストール。

$ gem install activerecord
Successfully installed activesupport-3.0.3
Successfully installed builder-2.1.2
Successfully installed i18n-0.5.0
Successfully installed activemodel-3.0.3
Successfully installed arel-2.0.6
Successfully installed tzinfo-0.3.23
Successfully installed activerecord-3.0.3
7 gems installed (...)

MySQL を使いたいので、普通の Ruby っぽく

$ gem install mysql

とやってみましたが、ダメでした。

$ gem install activerecord-jdbcmysql-adapter
Successfully installed activerecord-jdbc-adapter-1.0.3-java
Successfully installed jdbc-mysql-5.0.4
Successfully installed activerecord-jdbcmysql-adapter-1.0.3-java
3 gems installed (...)

こういうのを使う必要があるらしいです。
establish_connection のやり方も若干違います。

require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => 'jdbcmysql',
  :host => 'localhost',
  :username => 'root',
  :password => '',
  :database => 'piyopiyo_development',
  :pool => 5
)

あとは同じみたいです。

参考文献