# Prevent an object from being destroyed if it has related records

**URL:** <https://www.ruby-forum.com/t/prevent-an-object-from-being-destroyed-if-it-has-related-records/164183>\
**Category:** Rails\
**Created:** [March 29, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/prevent-an-object-from-being-destroyed-if-it-has-related-records/164183 "2009-03-29T18:11:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [March 29, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/prevent-an-object-from-being-destroyed-if-it-has-related-records/164183/1 "2009-03-29T18:11:11Z")

</div>

This is a total noob question, but how do you prevent a record from  
being destroyed if it has any related records. In my case, I have a  
book model and a page model. A book has\_many pages.

How do I prevent book.destroy from destroying a book if it has  
associated page records? Is there a :dependent option that would do  
this? Do I need to do some sort of before destroy callback?

Thanks!

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [March 29, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/prevent-an-object-from-being-destroyed-if-it-has-related-records/164183/2 "2009-03-29T18:31:07Z")

</div>

You could always use inheritance.

class Book \< ActiveRecord::Base  
def destroy  
return super if page.empty?  
return false  
end  
end

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [March 29, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/prevent-an-object-from-being-destroyed-if-it-has-related-records/164183/3 "2009-03-29T19:18:19Z")

</div>

Andrew B. wrote:

> You could always use inheritance.
> 
> class Book \< ActiveRecord::Base  
> def destroy  
> return super if page.empty?  
> return false  
> end  
> end  
> I think you can refactor it to:

def destroy  
page.empty? || super  
end

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [March 29, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/prevent-an-object-from-being-destroyed-if-it-has-related-records/164183/4 "2009-03-29T19:23:03Z")

</div>

> I think you can refactor it to:
> 
> def destroy  
> page.empty? || super  
> end

My bad:

page.empty? && super

untested though 🙂

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [March 30, 2009, 6:58am UTC](https://www.ruby-forum.com/t/prevent-an-object-from-being-destroyed-if-it-has-related-records/164183/5 "2009-03-30T06:58:50Z")

</div>

Neal L wrote:

> This is a total noob question, but how do you prevent a record from  
> being destroyed if it has any related records.

The database layer can do this easily, so harness its power! Put a  
foreign key constraint in your DB with ON DELETE RESTRICT. The Railsy  
way to do this is with the excellent foreign\_key\_migrations plugin (see  
[harukizaemon (Simon Harris) · GitHub](http://github.com/harukizaemon) ) – which you should probably be using  
in any case. It works with mySQL and PostgreSQL; I’m not sure about  
other database systems.

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)
