Has many ( THROUGH ) confusion

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.

Thanks

Keith

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
relationship.

Hope that helps,

Steve

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
course!

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

Keith