Miles to go …

February 13, 2008

TOTD #26: Overriding Database Defaults in Rails 2.0.2

Filed under: web2.0 — arungupta @ 12:02 am

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

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #35: Rails Database Connection on Solaris
  2. TOTD #24: Getting Started with Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1RC1
  3. TOTD #25: Rails application with PostgreSQL database using NetBeans
  4. TOTD #37: SQLite3 with Ruby-on-Rails on GlassFish Gem
  5. Database-enabled RoR WAR on GlassFish

5 Comments »

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

    Comment by Alex MacCaw — February 13, 2008 @ 12:55 am

  2. 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?

    WHY IS THERE NO COMPLETE STANDARD

    Comment by Chirdeep — February 13, 2008 @ 11:22 am

  3. Alex,

    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.

    Comment by Arun Gupta — February 14, 2008 @ 5:56 pm

  4. 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.

    Comment by gr — February 16, 2008 @ 11:24 am

  5. [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…

    Comment by Arun Gupta's Blog — July 1, 2008 @ 6:25 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress