A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time Rails developer and contributor, available for long- or short-term consulting, with solid experience in working as part of a distributed team. If you'd like to hire me, drop me a line. I'm also the author of Rails Rescue Handbook and Rails Freelancing Handbook.

Navigation

A Fresh Cup

Notes on Rails and other development

Tuesday
Oct072008

Routing Changes in Rails 2.2

For the most part, routing is unchanged in the upcoming Rails 2.2 release. But there are two changes that add a little extra syntactic sugar to your routes.

First, as Ryan Daigle covered last month, there's a new :shallow option for nested routes. This change goes a long way to answer the classic objections to deeply-nested routes. What shallow nesting does is give you additional route helpers. For example, with this declaration:

[sourcecode language='ruby']
map.resources :rooms, :shallow => true do |room|
room.resources :shelves do |shelf|
shelf.resources : books
end
end
[/sourcecode]

Any of these routes will be recognized:

[sourcecode language='ruby']
/rooms/1/shelves/2/books/3 ==> room_shelf_book_path(1,2,3)
/shelves/2/books/3 ==> shelf_book_path(2,3)
/books/3 ==> book_path(3)
[/sourcecode]

You can also combine shallow nesting with :has_one or :has_many for a more compressed syntax:

[sourcecode language='ruby']
map.resources :rooms, :has_many => { :shelves => :books }, :shallow => true
[/sourcecode]

The second change is that you can now supply an array of methods for new member or collection routes on resources. With Rails 2.1, you had to either pick a method or use the wildcard :any method. With Rails 2.2, you can specify just the verbs that a route really needs:

[sourcecode language='ruby']
map.resources :books, :collection => { :search => [:get, :post] }
[/sourcecode]
Monday
Oct062008

Double Shot #307

Now I know that getting to the Rapleaf API from Rails is trivial. I may never need this knowledge again.

  • Tunnel 1.1 - Nothing to do with Rails, really; just a cute game for MacBooks that uses the accelerometer as a control device.

  • Is the Hourly Model Broken? - Musings from RedMonk's Stephen O'Grady. He's writing from the analyst perspective, but this applies just as much to developers.

  • Release 0.8.0 - Of the Mack framework, that is.

  • Splitting models into several smaller files in Rails - I'm personally not convinced big models are a problem (I'd rather look in one large file than 6 small ones when tracing code). But if you disagree, here's a solution for loading the pieces.

  • Can't activate rubyforge (=0.4.5) problems - Adding a bit of Google juice to this solution (uninstall and reinstall) since I ran smack into the problem tonight.

Monday
Oct062008

Rails 2.2 Migration Goodies

With the imminent release of Rails 2.2, I've been spending a lot of time looking at the source code and git checkins. One area where developers will appreciate some small tweaks is migrations.

If you're not working on a multi-developer team, the default (UTC-based) numbering for migrations is a nuisance. You'll be able to change that by setting a configuration variable:

[sourcecode language='ruby']
config.active_record.timestamped_migrations = false
[/sourcecode]

That will give you the old, integer prefixes instead of the new fancy ones. Once you have those, it becomes easier to edit and re-run a specific migration, thanks to the fact that rake db:migrate:redo now takes an optional VERSION to specify the target migration to redo.

Finally, Rails 2.2 is getting transactional migrations - if you use PostgreSQL. Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn't applied. Also, the migration version was stored as having been executed, which means that it couldn't be simply rerun by rake db:migrate:redo after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone.
Monday
Oct062008

Upgrading MySQL 5.0 to 5.1 on OS X

I'm too lazy to build from source if there's not a good reason, and for running MySQL on OS X, I've yet to come across a good reason. So, I just use the MySQL Community downloads. Over the weekend I upgraded from 5.0.37 to the latest 5.1 download, because I was hard up against a MySQL bug that was fixed in later builds.

Unfortunately, while just running the installer for 5.1 worked great, it pointed the new server at a brand new set of databases - orphaning the couple of dozen databases I was working with. This was Not Good. The fix comes in two parts.

First, create /etc/my.cnf, with a single entry pointing to the old database files:

[mysqld]
datadir=/usr/local/mysql-5.0.37-osx10.4-i686/data

Second, tell MySQL to upgrade the files:

sudo mysql_upgrade -u root
Monday
Oct062008

Double Shot #306

My latest work in progress: The Rails Initialization Process.