This post first appeared on the Panter Blog:
blog.panter.ch/2016/01/12/spec-for-has_many-with-dependent-option.html
Every rails app almost certainly contains one or more has_many
associations.
Omitting the dependent
option means that on deletion the foreign key gets
nullified. In many cases this is not the expected behaviour though. Even if it
is, it makes sense that we think about the behaviour of the relation for each
has_many
definition. Not thinking about how the associated objects behave on
deletion often results in orphaned database entries and even in inconsistent
data.
Adding a simple test to the spec suite asserts that every has_many
association has explicitly defined the dependent
option.
First, we need two simple helper methods to get all our models and their
has_many
associations.
Then we should require these helper methods and expose them to our specs.
And finally, let’s add the spec that creates an example for each model.
The output could look something like the following in case we forget to define the option on one of the relations.
Now, every time we add a new model to our rails app and forget to define the
dependent
option we get a failing spec.