Forum: Ruby on Rails has many ( THROUGH ) confusion

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.
Df2330c56752967dd5a70d2280bae0c9?d=identicon&s=25 KJH (Guest)
on 2007-03-24 00:22
(Received via mailing list)
Maybe this is normal operation, but this doesn't seem right to me.

I have models:

- account
-- has many listshares
-- has many lists ( through listshares )

- list
-- has many listshares
-- has many accounts ( through listshares )

- listshare
--belongs to account
--belongs to list

I would have expected that if I perform the action list.destroy, then
any records in listshares with the associated list_id would be deleted
too.. but they are not.

If they should be, my model is wrong somewhere.  If not, I can stop
pondering, and code the listshares tidyup manually.


6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-03-24 01:15
(Received via mailing list)
You need another declaration in your listshares setup:

has_many :listshares, :dependent => :destroy

In the has many through join model, Listshare is a model in its own
right and we'll only be destroyed if you specify so in the

Hope that helps,

Df2330c56752967dd5a70d2280bae0c9?d=identicon&s=25 KJH (Guest)
on 2007-03-27 23:33
(Received via mailing list)
Do you mean that the LIST model should contain

has_many :listshares, :dependent => :destroy
? ( You said listshare)

When I put that in the LIST model,  it does remove listshare records
if a list is deleted, but now I'm confused about something else of

I don't always want to delete a list.. sometimes I want to just remove
the relationship between a list and an account - in sql terms to
delete from listhshare where account_id = x and list_id = y.

I can't work out how to do that ( apart from using sql )

Thanks in advance

This topic is locked and can not be replied to.