Sunday, September 12, 2010

JRuby and Rails 3

Rails 3.0 has been released. I know a number of you are excited about all of the possibilities. Along with the release of Rails is a new point release of JRuby.  So the big question is how do you use them together.

The default Rails environment uses SQLite as the database. Let me show you how to get started.
Riding on JRuby on Rails

  1. Start by downloading JRuby, and install it.
  2. Once it is installed you will want to install jruby-openssl.
    jruby -S gem install jruby-openssl
  3. Next lets install the SQLite database gems.
    jruby -S gem install jdbc-sqlite3 activerecord-jdbc-adapter \
    activerecord-jdbcsqlite3-adapter

  4. Once we have installed the gems for SQLite, we can install Rails.
    jruby -S gem install rails mongrel warbler
  5. Now that Rails is installed, we can create our example application. This will create our application.
    jruby -S rails new blog
  6. Next we replace the following line in the Gemfile

    gem 'sqlite3-ruby', :require => 'sqlite3'

    with the following:

    if defined?(JRUBY_VERSION)
    gem 'jdbc-sqlite3'
    gem 'activerecord-jdbc-adapter'
    gem 'activerecord-jdbcsqlite3-adapter'
    gem 'jruby-openssl'
    gem 'jruby-rack'
    gem 'warbler'
    else
    gem 'sqlite3-ruby', :require => 'sqlite3'
    end

    This allows us to run the application in JRuby, or Ruby.
  7. Next we need to modify the config/database.yml file.

    # SQLite version 3.x
    # gem install sqlite3-ruby (not necessary on OS X Leopard)
    development:
    adapter: jdbcsqlite3
    database: db/development.sqlite3
    pool: 5
    timeout: 5000

    # Warning: The database defined as "test" will be erased and
    # re-generated from your development database when you run "rake".
    # Do not set this db to the same as development or production.
    test:
    adapter: jdbcsqlite3
    database: db/test.sqlite3
    pool: 5
    timeout: 5000

    production:
    adapter: jdbcsqlite3
    database: db/production.sqlite3
    pool: 5
    timeout: 5000

  8. Next we need to migrate the database.
    jruby -S rake db:migrate
  9. Finally we can start our new Rails 3 application.
    jruby -S rails server
That's it. You are riding on Rails in 9 steps.

References:


Enhanced by Zemanta

12 comments :

Unknown said...

Instead of steps 5-7 you can simply do:

rails new blog --template http://jruby.org

which sets up Gemfile and database.yml for you.

John Yeary said...

Nick,

Your suggestion is very helpful, but it is not complete. I had to try it and check.

the --template option modifies line 5, and can replace line 6. I have some additional dependencies which are not absolutely necessary in line 6, but are helpful.

The database configuration was not changed using the --template option. So line 7 still stands.

John Yeary said...

The --template option created a Gemfile with the following modification.

if defined?(JRUBY_VERSION)
gem 'activerecord-jdbc-adapter'
gem 'jdbc-sqlite3', :require => false
else
gem 'sqlite3-ruby', :require => 'sqlite3'
end


The database.yml still points to sqlite3 instead of jdbcsqlite3

development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

The option as noted in my previous remark is helpful, and you provided an example I was missing. I also got to learn something new.

Thanks!

Scott T Weaver said...

I ran into some minor issues. For reference I am running this under Ubuntu 10.04 and RVM Jruby 1.5.3

1. Could not find gem 'bouncy-castle-java'. Easily fixed by installing the bouncy-castle-java gem.

2. Could not find mail-2.2.6.1 in any of the sources. Again, easily fixed by installing the mail gem.

3. I received this error when attempting to visit the site for the first time: "NameError (undefined method `attributes_with_quotes' for class `ActiveRecord::Base')" This took a little bit more effort. I ended up having to uninstall both activerecord-jdbc-adapter and activerecord-jdbcsqlite3-adapter, both of which where listed at version 0.9.2 and reisntall them, at which point the 0.9.7 versions were installed. I also had to change my Gemfile.lock to look for version 0.9.7 of both these gems.

The one thing I can think that may have been a contributing factor was that I initially created the site using MRI 1.9.2 (this included both a bundle install and a rake db:migrate).

John Yeary said...

Wow Scott, it looks like you had some real issues. I want to thank you for posting your comment with the issues, and the resolution. It helps everyone when you can post a solution.

John

LordM said...

Worked great for me. I used Ruby (not JRuby) 3 years ago with the standard Windows/Rails/MySQL setup.

This time I was trying to setup using Ubuntu, JRuby and Apache Derby. I wasted about 4 hours with various other web sites and endless out-of-date material (referring to client driver class etc), and couldn't get Derby working at all with Ruby or JRuby.

Here I just changed 'jdbcsqlite3' to 'jdbcderby' in database.yml, and in GemFile changed 'jdbc-sqlite3' to 'jdbc-derby'. Works out of the box!

John Yeary said...

There is nothing more exciting than positive remarks.

I am glad that it worked for you. I often post items based on issues I encounter and how I resolved it.

It is good to see that it helped you too!

Yonatan said...

does not work on Rails 3.0.4.rc1. If you have this version installed roll it back and install Rails 3.0.3 instead

red4d said...

Every example I've seen so far is for SQLite, and I need to set this up for MySQL. New to Rails and especially Jruby, couldn't get Rails 3 to run properly on Win 7 x64, so I was hoping to find a way to do this with Jruby.

Anyone tell me how to adapt these instructions for MySQL?

John Yeary said...

jruby -S rails new blog -d mysql

Modify the database.yml file like this:

development:
adapter: jdbcmysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
test:
adapter: jdbcmysql
encoding: utf8
reconnect: false
database: blog_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock

production:
adapter: jdbcmysql
encoding: utf8
reconnect: false
database: blog_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock


Modify the Gemfile to comment out mysql2 and include activerecord-jdbcmysql-adapter


# gem 'mysql2', '~> 0.2.6'

gem 'activerecord-jdbcmysql-adapter'


jruby -S rake db:create:all

jruby script/rails scaffold post title:string body:text published:boolean

jruby -S rake db:migrate

jruby script/rails server

red4d said...

Very nice! Thank you, that was like banging my head on a wall until I found your post. Worked like a charm.

John Yeary said...

You are very welcome. I am glad I could help. I appreciate your comments.

Popular Posts