Ruby on Rails - A Case Study

First a short diversion to put you in the right mood.

An old man, a boy and a donkey were going to town. The boy rode on the donkey and the old man walked. As they went along they passed some people who remarked it was a shame the old man was walking and the boy was riding.

The man and boy thought maybe the critics were right, so they changed positions.

Later, they passed some people that remarked, "What a shame, he makes that little boy walk."

They then decided they both would walk! Soon they passed some more people who thought they were stupid to walk when they had a decent donkey to ride. So, they both rode the donkey.

Now they passed some people that shamed them by saying how awful to put such a load on a poor donkey.

The boy and man said they were probably right, so they decide to carry the donkey. As they crossed the bridge, they lost their grip on the animal and he fell into the river and drowned.

The moral of the story?

If you try to please everyone, you might as well...
Kiss your ass goodbye.

The task at hand

I have in my hot hands a database with nearly 13,000 records (in the form of a CSV file). I thought, "it would be cool to load this into a MySQL database and rig up some kind of web interface so myself and others could enjoy it".

The first job is to bludgeon MySQL into submission (this is always how it feels to me to work with MySQL). I put together a quick schema, create a database, and import the csv into it. The import is lightning fast, so now I have a database loaded with cool stuff. I try to make a user with only select privileges granted, but this just does not work, so I give up on that for now and press on into setting up a rails application.

Setting up the servers

I hook this up live right away. I suppose more sensible souls would screw around with webrick and delay deployment till the end, but I do just the opposite, set this thing up right away to run in the real world. I edit my apache and lighttpd configuration, set up a virtual host and the proxy setup (as a duplicate of a happily running rails application), and restart both servers (lighttpd runs on port 8080 and apache uses mod_proxy to hand off requests for that virtual host).

Following the scheme in the rails book, I do this:

cd /u1/rails
rails micros
cd /u1/rails/micros/config
vi database.yml
cd /u1/rails/micros
ruby script/generate scaffold Mount Display
So far this just gives me a 404 error. I try stopping lighttpd and then I get a service unavailable error, so I know the proxy setup is doing the handoff. I start lighttpd again and poke around a bit. I find that I have the document root set up to be /www/rails/micros, but I need a symbolic link from there to /u1/rails/micros. After adding that, I get the rails greeting page, and know that I need to setup some kind of routing.

Make the scaffold work

Note that I have the following (among other things):

app/models/mount.rb
app/views/display.rb
app/controllers/display_controller.rb

Now, I get rid of public/index.html and add a line to config/routes.rb to try to make the "root" path be display/list:

map.connect '', :controller => "display", :action => 'list'
When I point my browser at the site, I just get 500 errors. Time to look at the server error log, which is NOT in /var/log/lighttpd. As specified in lighttpd.conf, it is in /u1/rails/micros/log/server.log

Aha, there is some trouble with permissions with something in /u1/rails/micros/tmp. (And, as a matter of fact, there was some such trouble with /u1/rails/micros/log.
Do this:

cd /u1/rails
chmod 777 log
chmod -R 777 tmp
Now we are off to the races, and the scaffold is showing the first few records in the database.

Make this more cool

Now the fun can start. Just paging 10 records at a time through a 12,633 line database is not going to be cool. We need to be able to search for specific things. We also want to get rid of controller methods that modify the database, since that is not the name of the game with this application. In particular, we get rid of the new, edit, update, and destroy methods. The show method fails, because it is not passing an ID. This is just my bad choice by not having the primary key record called "id" (I called it id_number). I think that by far the best thing to do here, is just change the schema. Since this is a read-only database, this takes all of about 10 seconds, and indeed, the show method now works along with list.
Feedback? Questions? Drop me a line!

Ruby on Rails notes / tom@mmto.org