Last updated January 18, 2011 04:54, by qmxme
[[Home|» JRuby Project Wiki Home Page]]
<h1>ActiveRecord-JDBC Database Support</h1>
ActiveRecord-JDBC 0.9.1 supports normal AR operations and basic migrations for the following databases:
* MySQL
* PostgreSQL
* SQLite3 (except <tt>change_column</tt>)
* Oracle
* HSQLDB (except <tt>migrations</tt> and <tt>habtm</tt> at least.)
* H2
* Microsoft SQL Server (except for <tt>change_column_default</tt>)
* DB2 (except <tt>change_column</tt>, <tt>change_column_default</tt>, <tt>rename_column</tt>, <tt>remove_column</tt>, <tt>add_index</tt>, <tt>remove_index</tt>, and <tt>rename_table</tt>)
* Derby (except <tt>change_column</tt>, <tt>change_column_default</tt>, <tt>remove_column</tt>, <tt>rename_column</tt>)
* FireBird (except <tt>change_column_default</tt> and <tt>rename_column</tt>)
==Usage==
Here's a walkthrough for MySQL in ultra-condensed form:
'''1.''' Grab a JRuby release from [http://jruby.org/download JRuby Downloads] and unpack it
'''2.''' Set up path for JRuby
export PATH=$PATH:[jruby-dir]/bin
'''3.''' Install Rails
jruby -S gem install rails
'''4.''' Install ActiveRecord-JDBC for MySQL (see [http://jruby-extras.rubyforge.org/activerecord-jdbc-adapter the documentation] for other options). This will also install the dependent gems.
jruby -S gem install activerecord-jdbcmysql-adapter
:You should see something like the following:
Successfully installed activerecord-jdbc-adapter-0.9.2
Successfully installed jdbc-mysql-5.0.4
Successfully installed activerecord-jdbcmysql-adapter-0.9.2
'''5.''' Generate a Rails App
rails ~/testapp --database mysql
'''6.''' Go to the app
cd ~/testapp
'''7.''' If you're using Rails 2.0 modify <tt>database.yml</tt> by prepending <tt>jdbc</tt> to the adapter name. For PostgreSQL, you'll need to add the <tt>host</tt> parameter as well. If you're NOT using Rails 2.0, don't modify your <tt>database.yml</tt> file.
'''ActiveRecord-JDBC version 0.9.0:'''
#SQLite3
development:
adapter: jdbcsqlite3
url: jdbc:sqlite:test.db # path to sqlite3 dbfile
'''ActiveRecord-JDBC version 0.9.1:'''
#SQLite3
development:
adapter: jdbcsqlite3
database: db/development.db
#MYSQL
development:
adapter: jdbcmysql
encoding: utf8
database: testapp_development
username: root
password:
#POSTGRES
development:
adapter: jdbcpostgresql
encoding: unicode
host: localhost
database: testapp_development
username: testapp
password:
#ORACLE
development:
adapter: jdbc
driver: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@myOracleHost:1521:mySID
username: myUser
password: myPass
#JavaDB/DerbyDB
development:
adapter: jdbc
driver: org.apache.derby.jdbc.EmbeddedDriver
url: jdbc:derby:{database};create=true
username: myUser
password: myPass
'''8.''' If you're using Rails 2.0, you're done. The rest of configuration, including the JDBC driver jar, is done automatically. If you're running Rails 1.2.x, you'll need to modify <tt>environment.rb</tt>. Add the following snippet inside the <tt>Rails::Initializer</tt> block:
if RUBY_PLATFORM =~ /java/
require 'jdbc/mysql' #per jdbc-mysql-5.0.4/README.txt
end
'''9.''' Create a <tt>testapp_development</tt> database, grants for <tt>testapp</tt> user, and a <tt>widgets</tt> table in MySQL (use migrations if you like).
'''10.''' Scaffold Widgets CRUD
jruby script/generate scaffold widget
'''11.''' Start up the server
jruby script/server
'''12.''' In your browser, go to [http://localhost:3000/widgets].
And that's about it. You've now got a scaffolded widget page running in JRuby over JDBC to MySQL.
==Troubleshooting==
* '''Scaffold fails with an error about "nonexistent jdbc adapter":''' Ensure that you've added the <tt>require</tt> line to <tt>environment.rb</tt>.
* '''Scaffold and script/server terminate without running:''' Make sure you've successfully installed the activerecord-jdbc-adapter gem. The additional <tt>require</tt> in <tt>environment.rb</tt> causes Rails scripts to die silently if there are any errors.
* '''Scaffold fails with the error "cannot convert NilClass into String":''' Make sure you've correctly specified the driver and url lines in <tt>database.yml</tt>
* '''You see the following error when using SQL Server:'''<br/><tt> com.microsoft.sqlserver.jdbc.AuthenticationJNI<br/> {clinit}<br/> WARNING: Failed to load the sqljdbc_auth.dll</tt><br/>See [[FAQs#troubleshooting_JNI|I am having a weird JNI problem. Help me!]]
* '''You have foxy_fixtures plugin installed and you get the following error:'''<br/><tt> 'load_missing_constant':<br/> uninitialized constant ActiveRecord::ConnectionAdapters::MysqlAdapter (NameError)</tt><br/>This error occurs because foxy_features ships with built-in active_record adapters for MySQL, SQLite and PostgreSQL. Specifying any of the <tt>jdbc:{dbname}</tt>'s, or just <tt>jdbc</tt>, will cause foxy_fixtures to load the MySQL Ruby driver by default. This, obviously, will fail since there is no native MySQL driver for JRuby. To be able to run the app, the easy fix is to take the plugin out—uninstall it. Development and testing with foxy_fixtures and <tt>activerecord-jdbc</tt> still needs to be researched.





