JRuby on Rails + ActiveRecord-JDBC with derbyを試してみる
前提
とりあえずRailsプロジェクトを作ってみる
D:\work\ruby\ActiveRecord-JDBC>jruby -S rails test create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create config/initializers create db create doc create lib create lib/tasks create log create public/images create public/javascripts create public/stylesheets create script/performance create script/process create test/fixtures create test/functional create test/integration create test/unit create vendor create vendor/plugins create tmp/sessions create tmp/sockets create tmp/cache create tmp/pids create Rakefile create README create app/controllers/application.rb create app/helpers/application_helper.rb create test/test_helper.rb create config/database.yml create config/routes.rb create config/initializers/inflections.rb create config/initializers/mime_types.rb create config/initializers/new_rails_defaults.rb create config/boot.rb create config/environment.rb create config/environments/production.rb create config/environments/development.rb create config/environments/test.rb create script/about create script/console create script/dbconsole create script/destroy create script/generate create script/performance/benchmarker create script/performance/profiler create script/performance/request create script/process/reaper create script/process/spawner create script/process/inspector create script/runner create script/server create script/plugin create public/dispatch.rb create public/dispatch.cgi create public/dispatch.fcgi create public/404.html create public/422.html create public/500.html create public/index.html create public/favicon.ico create public/robots.txt create public/images/rails.png create public/javascripts/prototype.js create public/javascripts/effects.js create public/javascripts/dragdrop.js create public/javascripts/controls.js create public/javascripts/application.js create doc/README_FOR_APP create log/server.log create log/production.log create log/development.log create log/test.log D:\work\ruby\ActiveRecord-JDBC>
とりあえずRailsプロジェクト作ってから、ActiveRecord-JDBCの設定をしてみる
おもむろにgem install
D:\work\ruby\ActiveRecord-JDBC>jruby -S gem install activerecord-jdbc-adapter JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL Successfully installed activerecord-jdbc-adapter-0.8.2 1 gem installed Installing ri documentation for activerecord-jdbc-adapter-0.8.2... Installing RDoc documentation for activerecord-jdbc-adapter-0.8.2... D:\work\ruby\ActiveRecord-JDBC>
config/database.ymlを変えなくちゃね。
元々の行はコメントアウトして、adapter以下の行を追加。
# adapter: sqlite3 # database: db/development.sqlite3 # timeout: 5000 adapter: jdbcderby username: password: hostname: localhost database: db/development
migrateしてみる
D:\work\ruby\ACTIVE~1\test>jruby -S rake db:migrate (in D:/work/ruby/ACTIVE~1/test) rake aborted! Please install the jdbcderby adapter: `gem install activerecord-jdbcderby-adapte r` (no such file to load -- active_record/connection_adapters/jdbcderby_adapter) (See full trace by running task with --trace) D:\work\ruby\ACTIVE~1\test>
エラーになった。。
activerecord-jdbcderby-adapterってのもインストールする必要があるらしい。
activerecord-jdbcderby-adapterもインストールする
D:\work\ruby\ACTIVE~1\test>jruby -S gem install activerecord-jdbcderby-adapter JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL Successfully installed jdbc-derby-10.3.2.1 Successfully installed activerecord-jdbcderby-adapter-0.8.2 2 gems installed Installing ri documentation for jdbc-derby-10.3.2.1... Installing ri documentation for activerecord-jdbcderby-adapter-0.8.2... Installing RDoc documentation for jdbc-derby-10.3.2.1... Installing RDoc documentation for activerecord-jdbcderby-adapter-0.8.2... D:\work\ruby\ACTIVE~1\test>
再びmigrate
D:\work\ruby\ACTIVE~1\test>jruby -S rake db:migrate (in D:/work/ruby/ACTIVE~1/test) D:\work\ruby\ACTIVE~1\test>
今度はうまくいった。
scaffold
芸のない、personモデルを作ってみる。属性は名前(name)だけ。
D:\work\ruby\ACTIVE~1\test>jruby script/generate scaffold person name:string JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/people exists app/views/layouts/ exists test/functional/ exists test/unit/ exists public/stylesheets/ create app/views/people/index.html.erb create app/views/people/show.html.erb create app/views/people/new.html.erb create app/views/people/edit.html.erb create app/views/layouts/people.html.erb create public/stylesheets/scaffold.css create app/controllers/people_controller.rb create test/functional/people_controller_test.rb create app/helpers/people_helper.rb route map.resources :people dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/person.rb create test/unit/person_test.rb create test/fixtures/people.yml create db/migrate create db/migrate/20080911043012_create_people.rb D:\work\ruby\ACTIVE~1\test>
migrateでpersonモデル(peopleテーブル)をDBに構築
D:\work\ruby\ACTIVE~1\test>jruby -S rake db:migrate (in D:/work/ruby/ACTIVE~1/test) == 20080911043012 CreatePeople: migrating ===================================== -- create_table(:people) -> 0.5898s -> 0 rows == 20080911043012 CreatePeople: migrated (0.5933s) ============================ D:\work\ruby\ACTIVE~1\test>
WEBrickを動かす
D:\work\ruby\ACTIVE~1\test>jruby script/server => Booting WEBrick... JRuby limited openssl loaded. gem install jruby-openssl for full support. http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL => Rails 2.1.1 application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options [2008-09-11 13:32:09] INFO WEBrick 1.3.1 [2008-09-11 13:32:09] INFO ruby 1.8.6 (2008-08-28) [java] [2008-09-11 13:32:09] INFO WEBrick::HTTPServer#start: pid=3300 port=3000
ちなみにDBの中身
E:\db-derby-10.4.2.0-bin\bin>ij ij バージョン 10.4 ij> connect 'jdbc:derby:D:\work\ruby\ActiveRecord-JDBC\test\db\development'; ij> select * from sa.people > ; ID |NAME |CREATED_AT |UPDATED_AT -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------------------------------- 1 |taka_2 |2008-09-11 04:32:55 .0 |2008-09-11 04:32:55.0 2 |taka_22 |2008-09-11 04:33:05 .0 |2008-09-11 04:33:05.0 2 行が選択されました ij> exit; E:\db-derby-10.4.2.0-bin\bin>