HABTM equivalent of :dependent?


#1

Hey guys :slight_smile:

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 :slight_smile:


#2

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


#3

On Mar 5, 5:43 pm, Xavier N. removed_email_address@domain.invalid wrote:

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

Indeed! :smiley: Thanks :slight_smile: