The default Rails environment uses SQLite as the database. Let me show you how to get started.
Riding on JRuby on Rails |
- Start by downloading JRuby, and install it.
- Once it is installed you will want to install jruby-openssl.
jruby -S gem install jruby-openssl
- Next lets install the SQLite database gems.
jruby -S gem install jdbc-sqlite3 activerecord-jdbc-adapter \
activerecord-jdbcsqlite3-adapter
- Once we have installed the gems for SQLite, we can install Rails.
jruby -S gem install rails mongrel warbler
- Now that Rails is installed, we can create our example application. This will create our application.
jruby -S rails new blog
- 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.
- 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
- Next we need to migrate the database.
jruby -S rake db:migrate
- Finally we can start our new Rails 3 application.
jruby -S rails server
References:
- JRuby:Wiki:GettingStarted
- JRuby and SQLite3 Living Together
- Deploy a Rails 3, Sqlite3 application in Tomcat using JRuby
12 comments :
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.
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.
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!
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).
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
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!
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!
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
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?
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
Very nice! Thank you, that was like banging my head on a wall until I found your post. Worked like a charm.
You are very welcome. I am glad I could help. I appreciate your comments.
Post a Comment