Unlike create!
, save!
, and friends, the destroy!
method didn’t exist in Rails 3.
It took me a while to get used to destroy
instead of destroy!
from the Rails console and from test code, but Rails 4 will provide destroy!
, which is different from destroy
in the following ways:
- Instead of returning
false
on failure, it will raiseActiveRecord::RecordNotDestroyed
- If you have a
before_destroy
callback that returns false, it will still raiseActiveRecord::RecordNotDestroyed
There’s going to be a lot less cycles of “write a test that calls destroy!
, see a NoMethodError
, change destroy!
to destroy
, and re-run the test” once Rails 4 is released. I know I’ve lost a few minutes of my life doing that :)
About the Author