RunningRailsWithActiveRecord JDBC

nicksieger edited this page Jan 6, 2011 · 4 revisions

ActiveRecord-JDBC Database Support

ActiveRecord-JDBC 0.9.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.9.1
    Successfully installed activerecord-jdbcmysql-adapter-0.9.1

5. Generate a Rails App

      rails ~/testapp --database mysql

6 . Go to the app

      cd ~/testapp

7. Modify database.yml by prepending "jdbc" to the adapter name. For PostgreSQL, you'll need to add the "host" parameter as well.

      #SQLite3
      development:
        adapter: jdbcsqlite3
        database: db/testapp_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        

8. Create a testapp_development database and grants for testapp user,

9. Scaffold Widgets CRUD

      jruby script/generate scaffold widget

10.

      jruby -S rake db:migrate

11. Start up the server

      jruby script/server

11. In your browser, go to http://localhost:3000/widgets

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

Troubleshooting

  • scaffold fails with an error about "nonexistent jdbc adapter" OR scaffold and script/server terminate without running: Make sure you've successfully installed the activerecord-jdbc-adapter gem.
  • 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.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.