From 79b38b8a71f7de20dc091c9e76ffd1c5d1686ea5 Mon Sep 17 00:00:00 2001 From: David Davis Date: Fri, 25 Jul 2014 15:58:34 -0400 Subject: [PATCH] Added rule about before_destroy for issue #7 --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 9a351cd3..43f3eec3 100644 --- a/README.md +++ b/README.md @@ -506,6 +506,32 @@ programming resources. end ``` +* + Since [Rails creates callbacks for dependent + associations](https://github.com/rails/rails/issues/3458), always call + `before_destroy` callbacks that perform validation with `prepend: true`. + + ```Ruby + # bad (roles will be deleted automatically by mistake) + has_many :roles, dependent: :destroy + + before_destroy :check_deletable + + def :check_deletable + fail "Cannot delete super admin." if super_admin? + end + + # good + has_many :roles, dependent: :destroy + + before_destroy :check_deletable, prepend: true + + def :check_deletable + fail "Cannot delete super admin." if super_admin? + end + ``` + + ## Migrations *