Forum: Ruby on Rails HABTM Dependency Question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jason Ketterman (Guest)
on 2005-12-30 06:12
(Received via mailing list)
I have a habtm relationship like the following:

Automobile <--> Options

What I want to do is when an option is deleted, make sure that there
are no Automobiles that still have that option. It seems simple
enough, but I am having a tough time with it. I believe Rails
automagically deletes the relationships when the automobile is
deleted, but not the other way around.

Any help would be appreciated!

Chris H. (Guest)
on 2005-12-30 15:43
(Received via mailing list)
auto1 = Automobile.create(...)
auto2 = Automobile.create(...)
cdplayer = Option.create(...)

# add the cdplayer to both autos
auto1.options << cdplayer
auto2.options << cdplayer

# show options

# doing this will delete the cdplayer option and also remove any
# since cdplayer no longer exists in the database

# doing this will delete the auto1 automobile and also remove any auto1
# since auto1 no longer exists in the database
This topic is locked and can not be replied to.