Finding a bug in Rails with your first ever line of code!

Posted by dave

Ruby on Rails

So my cousin-in-law (if there is such a word!), Simon Wheatley is starting to learn Rails and after writing his very first line of Rails code and struggling for a while eventually IM’ed me for some help. I must admit I assumed it would be a bug in his code, I mean who gets it right the very first time? However a little investigation revealed it was indeed a bug.

What was the line of code?

scaffold :nursery

Turns out singularisation was at fault, ActiveRecord and scaffolding use pluralize and singularize all over the place and “nursery” was not being singularised correctly.

>> "nurseries".singularize
=> "nurseries"

This was being caused by the regex in the rule for correctly serialising “series” to “series” being too loose and catching any word that ended in “series”.

So I spent a little time last night creating a patch for this. I hope it gets accepted but I fear it may not because additions/changes to singularisations have been rejected in the past. I am keeping my fingers crossed though because I have kept this patch very focused and it does fix a rule which is currently too loose.

That brings up an interesting point though, at what point do the inflector functions graduate from being utility functions for use in things like DB table name pluralisation into a full featured linguistic framework? Obviously a full linguistic framework needs to cope with multiple languages and maybe even context and I would agree this is way out of the intended scope of the current inflector. However I do feel that it could be expanded a little without straying into that territory.

For example while investigating this last night I came across a few examples that I feel should go in:

>> "foot".pluralize     => "foots" 
>> "feet".singularize   => "feet" 

>> "tooth".pluralize    => "tooths" 
>> "teeth".singularize  => "teeth" 

>> "goose".pluralize    => "gooses" 
>> "geese".singularize  => "geese"

I feel these, along with “nurseries”, “miseries” and the others that my patch fixes have as much right to be in there as “oxen” -> “ox” and “octopi” -> “octopus” which already have special cases in there. If this patch gets accepted, I might try my luck with this (patch here).

Anyway stick with it Simon, I am sure you won’t find a bug on every subsequent line :)

Comments

Leave a response

  1. SimonApril 05, 2007 @ 09:26 AM

    Thanks for your help Dave. All in all a good exercise, this is a learning project and I covered: remaking the project, generating and destroying controllers and models and adding custom singularisations to a project… all excellent educational points!

  2. AndyApril 10, 2007 @ 07:36 PM

    hah thats impressive, but not a fantastic first impression. Hope Simon sticks with it. Plus I crave for the day when I will need geese and teeth models. Thats going to be an awesome app :)