Thursday, January 25, 2007

Rails, Plural table names - lame

O.K. I’ve been working with Rails for several weeks now and am loving the ease for cranking out websites. Good stuff.

But…

(and that’s a big butt)

For some insane reason the developers of Rails decided plural table names was a convention worth promoting by having it as the default.

Most experienced DBAs will tell you that the name of a table should describe what one record in that table would represent. Who would recommend plural table names? Developers seem to like plural names because they equate database tables with Collections (think java.util.List). I guess I come from a more traditional background where I think of tables as set theory.

Naming guidelines aside there is another reason not to go down this route. Rails then has to be smart enough to parse the plurals for almost every noun in the English dictionary.

What short-circuited in the developer’s brains? They develop this slick environment and then slap in some hacky idea. I think it is only useful to add to the perceived “wow” factor that the Rails enthusiasts get at conferences. yee-ha!

Luckily there is a work-around to make Rails work just as nicely with singular table names.

Thanks to this post on justinram.wordpress.com for providing the answer on how to switch off this really stupid bug in rails:

Add the following to the bottom of your config/environment.rb file:

# Include your application configuration below
ActiveRecord::Base.pluralize_table_names = false

Design by convention

Final thing that “really grinds my gears” (Family Guy) is that it goes against the grain of “design by convention” that is Rails major contribution to the web space. Why would all my controllers, views, helpers, etc be called Employee and my table name be Employees?

Why would my code refer to “Sheep” and my table name be “Sheep”. Well, maybe that wasn’t a good example.

I guess in the end there is a difference between convention magic: “Hey, all my MVC objects are named the same so I don’t have to configure them - that makes sense” and spooky “under the covers” magic “Hey I magically figured out that your Alumnus class should be related to the Alumni table”

It is a good thing that Rails has that call to the Dictionary.com web service to get the plural name for every model.

8 comments:

Anonymous said...

I agree - a major downer for an otherwise fantastic method of web app development!

Mike Vargo said...

Get over yourself. It reads so much easier to use the plural. I just created a model called Delivery and got a table called deliveries. No sweat.

person => people. It's nice.

Anonymous said...

Arrrgh! What a pain. They have aimed at producing a framework to make life easier, but with this little complication they have made my life harder. Now I have to check to see what rails will call each table if the pluralization is in anyway non obvious.

Anonymous said...

Read an introduction for RoR and also was surprised.

Well, for some foreigners it is not obvious that a table for Person would be people.

The next step could be adding some polotical correctness check and silent automatic renaming of classes

Michael Nissim said...

This might be useful to some: Sometimes you have to override the table name Rails is looking for. For instance, to use a table name in singular form, when rails is looking for the plural form, just put this in the model:

class Dog < ActiveRecord::Base
set_table_name 'dog'
end



This will prevent it from looking for the table "dogs".

I found this in:
http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord/Naming#Set_use_pluralization

Unknown said...

Don't get over yourself. Plural database names don't make sense. Thanks for the tip on how to change it.

Kuruvi said...

Not much has changed in years...
When I created a model called UnitOfMeasure, I got a table named Unit_Of_Measures. Units_Of_Measure would have been better :)

Anonymous said...

Totally agree. Have a table: device_history . device_histories ??