# Handling database table dependencies

**URL:** <https://www.ruby-forum.com/t/handling-database-table-dependencies/163220>\
**Category:** Rails\
**Created:** [March 18, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/handling-database-table-dependencies/163220 "2009-03-18T12:06:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 18, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/handling-database-table-dependencies/163220/1 "2009-03-18T12:06:04Z")

</div>

Hey,

thanks for reading this post. How do you handle dependencies? A really  
simple example: I’ve got a table called “entity” and another table,  
let’s say, “entity\_type”. The entity types can be changed, of course,  
they also should be deletable, but only, if they are not used by some  
entities! How can I ensure this in Rails or directly in the DBMS  
(postgres)? Do I have to check it manually within a validate-method in  
the model?

Thanks for any suggestions!

Cheers, ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 19, 2009, 6:19am UTC](https://www.ruby-forum.com/t/handling-database-table-dependencies/163220/2 "2009-03-19T06:19:41Z")

</div>

Are you talking about a polymorphic association??

You can set that up simply enough in the models after doing a wee  
search on them,

[http://envycasts.com/products/advanced-activerecord](http://envycasts.com/products/advanced-activerecord)

May have some interesting insight into them, it would then be easy  
enough to create a method from a before\_destroy callback to check if  
it is being used or not…

Cheers

Richard
