Forum: Ruby on Rails HABTM equivalent of :dependent?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3d197df430dbfd0d0b11f23b0a8d3ec9?d=identicon&s=25 Daniel Smedegaard Buus (Guest)
on 2007-03-05 17:16
(Received via mailing list)
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 :)
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-03-05 17:46
(Received via mailing list)
On Mar 5, 2007, at 5:10 PM, Daniel Smedegaard Buus 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
3d197df430dbfd0d0b11f23b0a8d3ec9?d=identicon&s=25 Daniel Smedegaard Buus (Guest)
on 2007-03-06 09:23
(Received via mailing list)
On Mar 5, 5:43 pm, Xavier Noria <f...@hashref.com> wrote:
>
> This is the canonical example for polymorphic associations :-).
>

Indeed! :D Thanks :)
This topic is locked and can not be replied to.