Skip to content

Commit

Permalink
Added rule about before_destroy for issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
David Davis committed Jul 25, 2014
1 parent e36fdf7 commit e9df89c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,32 @@ programming resources.
end
```

* <a name="before_destroy"></a>
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 even if super_admin? is true)
has_many :roles, dependent: :destroy
before_destroy :ensure_deletable
def ensure_deletable
fail "Cannot delete super admin." if super_admin?
end
# good
has_many :roles, dependent: :destroy
before_destroy :ensure_deletable, prepend: true
def ensure_deletable
fail "Cannot delete super admin." if super_admin?
end
```


## Migrations

* <a name="schema-version"></a>
Expand Down

0 comments on commit e9df89c

Please sign in to comment.