Written by Jared Haworth on
I was very excited to use the new Advanced YAML Fixtures introduced in Rails 2.0, so I set about updating my code in an Edge Rails application I’ve been working on for the past year or so. There are a few caveats in using the new fixture styles, though.
The Fixtures are introspective, so if you’ve overridden the default name of an association in your model, you’ll need to use that new name in your fixture. With the following code,
class Worker < ActiveRecord::Base
belongs_to :status, :class_name => 'WorkerStatus', :foreign_key => 'worker_status_id'
...
end
The key for worker_status: should actually just be status: in the workers.yml fixture file.
The larger problem I encountered was in using a mix of IDs and fixture record names. I use a testing framework, TestRig, which was written by Mike Clark and Dave Thomas for the Pragmatic Studio. The TestRig framework takes in ID parameters of fixtures in the database, and loads them accordingly. This falls apart with the new status: full_time style of declaring relationships in fixtures, because the former relies on a ‘known’ ID, and the new style generates a fairly random ID instead. This leads to a bunch of broken relationships throughout the test suite.
According to the API documentation, the generated ID is constant, so I could discover that ID and use it in the TestRig calls which require an integer key, but then I’ve essentially lost all benefit of using the Advanced YAML Fixtures, and added some crazy complexity to my tests as well.