I’ve run across an interesting scenario with my migrations, and I hope
you might share some insight.
Let’s say I have 3 migrations and two Models (Person and Car):
The first creates the initial schema…the second loads default data
using model objects of Person to do so, and the third creates a new
table, “cars”, and modifies the “persons” table to create a :has_one
relationship of Person->Car.
Let’s also say I want all Persons to have at least one car, so I change
my Person model to have:
self.car = Car.new unless self.car
Now, what happens, is that when I issue a “rake migrate” on my intial
When I start from scratch (new database) and do a rake migrate, my data
load fails because it uses the model class Person, yet Person’s code now
has an after_initialize that expects the Car association to be present.
Because Car isn’t created until migration 003, I’m essentially up the
creek. rake throwns an InvalidStatement exception.
Has anyone found a decent way of handling these scenarios?