I understand that according to Rails philosophy, data integrity checks should be done at the application level as opposed to the database level. Like many other developers, I enthusiastically disagree.
I’ve found a lot of discussions addressing this problem but they all seem to be old and, dismayingly, they seem to point to divergent solutions.
I have to imagine there’s a de-facto standard way of doing foreign key constraints in Rails 3. However, whatever it is (if it does exist) seems to be smothered by all the past discussions because I can’t find it.
Are Rails developers by this point mostly on the same page with foreign keys? If so, I would love to know how they’re generally handled.
It is for this reason that I (and the people who wrote Enterprise Rails – http://oreilly.com/catalog/9780596515201) recommend that you write your entire up and down migrations in SQL.
The advantages are:
There are disadvantages:
But, overall I reckon the advantages outweigh the disadvantages.
Quick example: