# Has many ( THROUGH ) confusion

**URL:** <https://www.ruby-forum.com/t/has-many-through-confusion/89943>\
**Category:** Rails\
**Created:** [March 24, 2007, 12:22am UTC](https://www.ruby-forum.com/t/has-many-through-confusion/89943 "2007-03-24T00:22:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KJH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@KJH](https://www.ruby-forum.com/u/KJH)\
**Post date:** [March 24, 2007, 12:22am UTC](https://www.ruby-forum.com/t/has-many-through-confusion/89943/1 "2007-03-24T00:22:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KJH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@KJH](https://www.ruby-forum.com/u/KJH)\
**Post date:** [March 24, 2007, 1:15am UTC](https://www.ruby-forum.com/t/has-many-through-confusion/89943/2 "2007-03-24T01:15:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KJH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@KJH](https://www.ruby-forum.com/u/KJH)\
**Post date:** [March 27, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/has-many-through-confusion/89943/3 "2007-03-27T23:33:13Z")

</div>

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
