Skip to content

Commit

Permalink
Merge pull request #110 from daviddavis/before_destroy
Browse files Browse the repository at this point in the history
Added rule about before_destroy for issue #7
  • Loading branch information
bbatsov committed Aug 4, 2014
2 parents e36fdf7 + e9df89c commit 87ddc25
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 87ddc25

Please sign in to comment.