Sans rails my normal approach to handling this would be to enforce it in
the database and when a database operation failed I’d try to validate
the model to produce an error message. It’s always served me pretty
Rails expects you to validate before commiting but of course this does
nothing to guarantee referential integrity. It’s perfectly possible for
one process to alter the database between another processes validation
and commit. Or am I missing something?
Lets say I go with the flow anyway but try to fix this short comming by
using the database to enforce referential integrity. So I find a plugin
that adds foreign key support to migrations. All seems good so far…
Of course this means that I have to deal with loading fixtures in the
correct order but since rails loads them in the order specified by the
tests that’s no problem.
and finally the question…
Why doesn’t the test database honor the constraints? (postgres in this
case) Is there a way to disable this behavior? If my test database
correctly handled constraints what other problem would it create? I
don’t want my test environment operating differently that my production
Or maybe an even better questions…
What does everyone else do to enforce referential integrity, I’m not
interested in bucking the system or reinventing the wheel?