Using STI with rails fixtures [TECH]
Wednesday, February 20th, 2008This is the first of many technical posts to come. I’ve added TECH in the title to allow non-technical readers to ignore these posts at a glance.
I’m using single table inheritance (STI) pretty heavily in the rails apps I’m building. It makes complete sense to me and keeps everything organized. Unit testing is new to me and so I left it alone for a while. When I went to add fixtures to my STI models (which are all Lord of the Rings references thank you very much), I realized I didn’t know what to do. I did some research and thought I was so smart. I talked to another dev and he recommended just changing the class name from the unit test, but we ran into a problem. We need to be able to load our fixtures without the unit tests themselves. If I did that, it would try to load the fixtures into database tables that don’t exist. So, we did some research and discovered my initial research is right. Even with STI, the filename of the fixtures has to be the database table name that it will get loaded into. Then you just set the type correctly in the fixture and it gets loaded into the right object.
Aside: I’m posting this from TextMate as well. Since I’m programming my RoR apps in TextMate, seems easy enough to just post from here when I find something interesting.
Pictures (Flickr)
Genealogy
Pas de Deux