TOTD #26: Overriding Database Defaults in Rails 2.0.2

A Rails 2.0.2 application configures SQLite3 database by default. This requires you to have SQLlite3 database running and also have the corresponding database adapter installed. Even this is part of the Leopard (Mac OS 10.5+) development kit but this needs extra configuration on non-Leopard machines.

One of the guiding principles of Rails is Convention-over-Configuration. So there are couple of ways you can override this default:

  1. Generate the Rails app by specifying the database of your choice. So if you want to use MySQL as the default database, then generate the application as:

    jruby -S rails -d mysql myapp

    This is assuming you are using JRuby for generating your Rails application.
  2. If you want to use Rails without a database, remove ActiveRecord framework by editing "config/environment.rb" and adding the line:

    config.frameworks -= [ :active_record ]

    Alternatively you can uncomment line 21 in "environment.rb" and remove other frameworks from inside the parentheses.
This is generally handy if you want to generate and test a simple Rails application that does not involve database.

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

Technorati: totd rubyonrails jruby ruby leopard sqlite3 database

Are we going to get Sqlite3 support in JRuby anytime soon?

Posted by Alex MacCaw on February 12, 2008 at 04:55 PM PST #

I have few questions? I hope you can answer them. Maybe this is not the WRITE section but....

why is market drifting towards .net? dot net or java?

instead of supporting ruby, why cant sun make java and tools easier? ANT what is that for? progammer shuld only write business logic not an ANT script.

why struts, hibernate, spring, shale? JSF but industry still moans about Struts.

is open source really helping?


Posted by Chirdeep on February 13, 2008 at 03:22 AM PST #


Would you be interested in working on it ? :)

The shortest path would be to use one of the sqlite jdbc drivers out there. But it will probably be difficult to get working out of the box because the sqlite adapter that's in Rails itself will take precedence.

Posted by Arun Gupta on February 14, 2008 at 09:56 AM PST #

You said:

"This requires you to have SQLlite3 database running"

Ummm, there is no such thing. You imply that sqlite3 is a running process. It is not. It is simply a file on the filesystem that gets created automatically by the first call to the DB.

Do you often find you are creating rails apps that don't use a database? Can you give an example? Plain Ruby apps I can understand, but I think there are very few (none) rails apps that make no use of a DB.

You said:

"But it will probably be difficult to get working out of the box because the sqlite adapter that's in Rails itself will take precedence"

There is no sqlite3 adapter built into rails AFAIK. It is simply a standard Ruby gem library that comes pre-installed on OS X and many other systems and is independant of Rails. It is really an add on component of Ruby that Rails makes use of. If you can install the compiled sqlite3 lib and the sqlite3-ruby gem, you are good to go.

The point of all this is to create a simple default setup that is generally easier to configure and get running than mysql is when starting initial development (e.g. there is no separate step to create the db, it is autocreated). I don't think there are any/many in the rails community that would suggest running a production app on sqlite3 though. Rails migrations make the process of switching eventually from sqlite3 to mysql (or any other Rails supported DB) trivial (just change the name of the database adapter in database.yml and re-run the migrations).

This was kind of a useless and factually incorrect article (even with its tiny bit of actual content) IMHO. It seems you are targeting first-day developers, but then seek to confuse them by throwing in gratuitous references to Sun's work on jruby as if thats the default. This approach would seem to only further confuse any developer that actually needed this ultra basic level of article. I have nothing against JRuby at all but I think this article just sought to add one more link on the web that links JRuby and Rails in a Google search. That seems kind of cynical to me. Target users and not search engines.

Sorry, I hope the next try is better. I'd love for Sun to be active in this community and post real (as in useful) information and code.

Posted by gr on February 16, 2008 at 03:24 AM PST #

[Trackback] I've created a Rails "Hello World" app numerous times. But I decided to write a simple using the testing framework provided by Rails. This blog explains my experience of writing such a test. Create a "Hello World" app as: ~/samples/jruby/test...

Posted by Arun Gupta's Blog on June 30, 2008 at 11:25 PM PDT #

Post a Comment:
Comments are closed for this entry.

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.

Java EE 7 Samples

Stay Connected


« July 2016