Running Rails with ActiveRecord-JDBC

From JRubyWiki

Jump to: navigation, search

ActiveRecord-JDBC Database Support

ActiveRecord-JDBC 0.8.1 supports normal AR operations and basic migrations for the following databases:

  • SQLite3
  • MySQL
  • PostgreSQL
  • Oracle
  • HSQLDB (except migrations and habtm at least. I gave up)
  • H2
  • Microsoft SQL Server (except for change_column_default)
  • DB2 (except change_column, change_column_default, rename_column, remove_column, add_index, remove_index and rename_table)
  • Derby (except change_column, change_column_default, remove_column, rename_column)
  • FireBird (except change_column_default and rename_column)

See the documentation for more information.

Usage

Here's a walkthrough for MySQL in ultra-condensed form:

1. Grab a JRuby release: Main_Page#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 the documentation for other options). This will also install the dependent gem: activerecord-jdbc-adapter.

     jruby -S gem install activerecord-jdbcmysql-adapter

You should see something like the following:

   Successfully installed activerecord-jdbc-adapter-0.7.1
   Successfully installed activerecord-jdbcmysql-adapter-0.7.1

If the RubyForge gem repositories do not yet have v0.7.1 or later of these gems you can try installing it from here:

    jruby -S gem install activerecord-jdbcmysql-adapter --source http://caldersphere.net

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 database.yml by prepending "jdbc" to the adapter name. For PostgreSQL, you'll need to add the "host" parameter as well. If you're NOT using Rails 2.0, don't modify your database.yml file.

     #SQLite3
     development:
       adapter: jdbcsqlite3
       url: jdbc:sqlite:test.db # path to sqlite3 dbfile
     #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
       

8. If you're using Rails 2.0, you're done. The rest of configuration (including JDBC driver jar) is done automatically. If you're running Rails 1.2.x, you'll need to modify environment.rb. Add the following snippet just above the Rails::Initializer line:

     if RUBY_PLATFORM =~ /java/
       require 'rubygems'
       gem 'activerecord-jdbcmysql-adapter'
       #require 'active_record/connection_adapters/jdbcmysql_adapter'
       require 'jdbc_adapter' # Changed to comply with this FAQ question.
     end

9. Create a testapp_development database, grants for testapp user, and widgets 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/widget

And that's about it. You've got a scaffolded widget page running in JRuby over JDBC to MySQL.

Troubleshooting

  • ActiveRecord-JDBC does not install: I had some trouble myself; I think something's up with either RubyForge or with our released gem. You can always download ActiveRecord-JDBC and install it locally, of course.
  • scaffold fails with an error about "nonexistent jdbc adapter": Ensure you've added the require line to environment.rb (hopefully this will be unnecessary in the future)
  • scaffold and script/server terminate without running: Make sure you've successfully installed the ActiveRecord-JDBC gem. The additional require in environment.rb 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 database.yml
  • "unable to choose type from ... for decimal": postgresql and oracle don't report a type for decimal. Fixed in ActiveRecord-JDBC trunk.
  • If you are using Microsoft SQL Server and your migration do not work or you data is all String instead of the proper type, check this issue JRUBY-1352 for a workaround.
  • if you see this error when trying to run a migration:
 rake aborted!
 undefined method `last' for {}:Hash

it probably means you had previously used the 0.7.0 version of the activerecord-jdbcmysql-adapter and it had created the following rake task file in your rails application:

 lib/tasks/jdbc_databases.rake
  • net/ssh doesn't work at this time with jruby 1.0.3 and net/ssh 1.1.2

Delete this file and the error should go away. See: JRUBY-1859 for more info.

com.microsoft.sqlserver.jdbc.AuthenticationJNI
<clinit>
WARNING: Failed to load the sqljdbc_auth.dll
  • You have foxy_fixtures plugin installed and you get the following error:

'load_missing_constant': uninitialized constant ActiveRecord::ConnectionAdapters::MysqlAdapter (NameError).

This error occurs because foxy_features ships with built-in active_record adatpers for mysql, sqlite and postgresql. Specifying any of the jdbc<dbname> or just jdbc 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). Development and testing with foxy_fixtures and activerecord-jdbc still needs to be researched.

Personal tools