# HABTM equivalent of :dependent?

**URL:** <https://www.ruby-forum.com/t/habtm-equivalent-of-dependent/87732>\
**Category:** Rails\
**Created:** [March 5, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/habtm-equivalent-of-dependent/87732 "2007-03-05T17:16:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 5, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/habtm-equivalent-of-dependent/87732/1 "2007-03-05T17:16:07Z")

</div>

Hey guys 🙂

I’m wondering how I would go about getting a :dependent behavior on a  
habtm relation… It seems logical enough not to have :dependent on  
habtms, because habtm usually has to do with two different models that  
are “valid in themselves, independently of each other”, or how to put  
it…

But, in this particular case, the models are actually dependent. What  
I have are users, businesses, and addresses. A user can have a number  
of addresses, and they will belong to this user only. Likewise, a  
business can have a number of addresses, which again belong only to  
the business.

So, I really do need the M2M-table but it’s not really a “has and  
belongs to many” relationship, it’s actually more like a “user has  
many addresses, but so do business, and thus I can’t have a foreign  
key to user in an address”.

Finally, when I delete a user, I’d like for the addresses to be  
deleted as well. But I don’t have a :dependent option…

What would you do?

Thanks in advance,  
Daniel 🙂

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 5, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/habtm-equivalent-of-dependent/87732/2 "2007-03-05T17:46:13Z")

</div>

On Mar 5, 2007, at 5:10 PM, Daniel Smedegaard B. wrote:

> the business.
> 
> So, I really do need the M2M-table but it’s not really a “has and  
> belongs to many” relationship, it’s actually more like a “user has  
> many addresses, but so do business, and thus I can’t have a foreign  
> key to user in an address”.
> 
> Finally, when I delete a user, I’d like for the addresses to be  
> deleted as well. But I don’t have a :dependent option…

This is the canonical example for polymorphic associations :-).

– fxn

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 6, 2007, 9:23am UTC](https://www.ruby-forum.com/t/habtm-equivalent-of-dependent/87732/3 "2007-03-06T09:23:01Z")

</div>

On Mar 5, 5:43 pm, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is the canonical example for polymorphic associations :-).

Indeed! 😃 Thanks 🙂
