Day one of my new Rails application (let’s call it mystats) has been very productive. In a few hours, I have a complete set of (ugly) web pages for entering and browsing directly through my tables.
The first observation I had was that Rails assumes that your database tables are named in the plural. So, for example, it expects a table named “registered_users” instead of “registered_user”. This is mildly annoying to me, since I’ve always considered the “s” to be implied and therefore redundant, but I’m trying to be Rails-y here, so I’ll go with it. One thing that is
cool, though, is that it correctly converts “person” to “people”.
I also initially fought the Ruby convention of using underscores in table names. I’ve traditionally named tables by using all lowercase, with all the words smashed together (eg registerduser instead of registered_user). This turned out
to cause a little bit of a mess when I generated my model (eg. I ended up with a class called Registereduser instead of RegisteredUser), so I had to finally cave in.
I also learned something about MySQL, which is that if you use the MyISAM engine, you can’t have foreign keys. Annoyingly, it will let you try to specify one in your script and happily execute it with no errors. You just won’t end up with the foreign keys you were expecting.
The default pages that Rails generates don’t do anything special for fields that have foreign key relationships. What would be nifty is if it automagically generated a drop-down or a link to a search page. Maybe there are plugins for that, who knows.
My first bit of research will be to figure out how to do many-to-many relationships.
For example, I’ve got a many-to-many relationship between people and groups that I’ve
represented in the database with a mapping table:
people 1->n person_group n<-1 group
Rails generated a different class for each table, but in reality I’d prefer my object
model to only have a Person and a Group, with each of them containing a collection
of the other. Maybe that, too, isn’t very Railsy, but I guess I’ll find out.
Over all, though, it was a very productive day as Rails promises. I bought a book today so I can be more productive on the bus to and from work–hopefully that means even more progress to come!