We had an interesting discussion on my team when doing the first upcase challenge, but I completely forgot to ask about it:
We had an association where
User has_many :books and I wanted to just add
dependent: :destroy, but someone else brought up adding the
foreign_key option to the migration.
When the database deals with cascading the foreign key deletion, there’s no need for the
dependent: :destroy option on the association and the simple
shoulda matcher for testing that option will not work since the application does not know about the database constraint.
I’ve had enough problems with orphaned foreign keys that I like to have some test coverage, so we added a simple spec, but that approach isn’t really going to scale very well as more foreign key constraints are added to the app.
How/do you test database constraints like this? With no
dependent: :destroy in the model, a new developer on the project would have to be familiar with the schema to know that the database is handling that logic and the only real protection from a regression is a code review (although I guess it’s probably not very likely that someone will try to remove the foriegn_key constraint once it’s added).