# How to restrict deletion of ActiveRecord objects if there de

**URL:** <https://www.ruby-forum.com/t/how-to-restrict-deletion-of-activerecord-objects-if-there-de/110684>\
**Category:** Rails\
**Created:** [July 29, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/how-to-restrict-deletion-of-activerecord-objects-if-there-de/110684 "2007-07-29T17:13:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 29, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/how-to-restrict-deletion-of-activerecord-objects-if-there-de/110684/1 "2007-07-29T17:13:38Z")

</div>

Hi guys,

Trying to find some way to restrict deletion of ActiveRecord objects  
if there dependent records.

I’m thinking of using before\_destroy for it, but don’t know how to  
“cancel” deletion in my code in before\_destroy “decides” that deletion  
should be restricted.

Thank you in advance.

–  
Regards  
Yuriy Padlyak

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 30, 2007, 1:18am UTC](https://www.ruby-forum.com/t/how-to-restrict-deletion-of-activerecord-objects-if-there-de/110684/2 "2007-07-30T01:18:37Z")

</div>

gneeot wrote:

> I’m thinking of using before\_destroy for it, but don’t know how to  
> “cancel” deletion in my code in before\_destroy “decides” that deletion  
> should be restricted.

According to the Rails doc, all you need to do is return false from your  
before\_destroy method. Here’s the relevant section:

# == Cancelling callbacks

# 

# If a before\_\* callback returns false, all the later callbacks and

the associated action are cancelled. If an after\_\* callback returns

# false, all the later callbacks are cancelled. Callbacks are

generally run in the order they are defined, with the exception of  
callbacks

# defined as methods on the model, which are called last.

Hope this helps,

Adam

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 30, 2007, 4:43am UTC](https://www.ruby-forum.com/t/how-to-restrict-deletion-of-activerecord-objects-if-there-de/110684/3 "2007-07-30T04:43:02Z")

</div>

Hi Yuryi,

> Trying to find some way to restrict deletion of ActiveRecord objects  
> if there dependent records.
> 
> I’m thinking of using before\_destroy for it, but don’t know how to  
> “cancel” deletion in my code in before\_destroy “decides” that deletion  
> should be restricted.

I’m pretty sure that if a “before\_” filter returns false, the whole  
action  
is canceled. Check the class docs at [api.rubyonrails.org](http://api.rubyonrails.org) to be sure.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [August 6, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/how-to-restrict-deletion-of-activerecord-objects-if-there-de/110684/4 "2007-08-06T18:23:47Z")

</div>

it works great, thank you.

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 30, 2007, 10:43am UTC](https://www.ruby-forum.com/t/how-to-restrict-deletion-of-activerecord-objects-if-there-de/110684/5 "2007-07-30T10:43:11Z")

</div>

thanks, will try it.
