# Has\_many :through with join table as a has\_many FK issue

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-join-table-as-a-has-many-fk-issue/172544>\
**Category:** Rails\
**Created:** [July 23, 2009, 4:45am UTC](https://www.ruby-forum.com/t/has-many-through-with-join-table-as-a-has-many-fk-issue/172544 "2009-07-23T04:45:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 23, 2009, 4:45am UTC](https://www.ruby-forum.com/t/has-many-through-with-join-table-as-a-has-many-fk-issue/172544/1 "2009-07-23T04:45:08Z")

</div>

I have a join table:

sitedata  
belongs\_to :tb1  
belongs\_to tb2

has\_many :tb3

When I add a relationship in the join table I then add data to the tb3  
table and that populates the FK in tb3 with the id of the join table.  
no problems there.

when I delete the row in the join table the data stays in the tb3  
table. how do I get the tb3 data to delete?

Is not has\_many supposed to delete the data on the other end when the  
primary table no longer has a reference?

If I go into the DB and make the FK a constraint with a delete on  
cascade it works when the data in the join table is deleted.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 23, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/has-many-through-with-join-table-as-a-has-many-fk-issue/172544/2 "2009-07-23T12:10:28Z")

</div>

Your example doesn’t make much sense from here, but you’ll probably  
want to look into the various :dependent options to has\_many.

–Matt J.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 23, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/has-many-through-with-join-table-as-a-has-many-fk-issue/172544/3 "2009-07-23T19:53:02Z")

</div>

you have to add :dependent =\> :destroy in the association

you B table has\_many A

then the B model will looks like

has\_many :A , :dependent =\> :destroy

so when you destroy an object of B it will automatically delete all  
the As that was associated with that B

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 23, 2009, 7:59pm UTC](https://www.ruby-forum.com/t/has-many-through-with-join-table-as-a-has-many-fk-issue/172544/4 "2009-07-23T19:59:28Z")

</div>

Here is the layout,

TBa -\> TBb -\> TBc  
|  
TBd

TBa has\_many TBc :through =\> :TBb

TBb is the join table.

TBb has\_many :TBd, :dependent =\> destroy.

TBd data stays there even though the data in TBb is deleted.

In my program I make the relationship dynamically and then I add data to  
TBd  
for the relationship in TBb.

When I delete the relationship in the join table the data still remains  
in  
TBd.
