# Noob question on deleting from join tables

**URL:** <https://www.ruby-forum.com/t/noob-question-on-deleting-from-join-tables/73947>\
**Category:** Rails\
**Created:** [February 26, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/noob-question-on-deleting-from-join-tables/73947 "2006-02-26T23:12:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 26, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/noob-question-on-deleting-from-join-tables/73947/1 "2006-02-26T23:12:38Z")

</div>

When I have a habtm association between two models (User & List),  
implemented  
thru a join table users\_lists, there are a variety of ways to “delete”  
associations between specific User\<-\>List combinations.

ActiveRecord lists delete, clear etc.

However, these methods all operate by setting foreign keys in the join  
table  
to nil and do not remove rows from the join table.

Q1: isn’t the join table going to contain a LOT of useless records with  
nil as  
one column value after a period of time (after deleting many individual  
User/List associations etc.)

Q2. is there a way to break an association between a specific User/List  
pair  
by DELETING the row in the join table? (instead of setting a foreign key  
column value to NIL)

thanks, can’t find an answer to this thru usual sources. In fact, there  
is  
almost no examples I could find of deleting associations for HABTM  
(quite a  
few examples now for setting them up though)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [February 27, 2006, 12:31am UTC](https://www.ruby-forum.com/t/noob-question-on-deleting-from-join-tables/73947/2 "2006-02-27T00:31:22Z")

</div>

Chris wrote:

> When I have a habtm association between two models (User & List), implemented  
> thru a join table users\_lists, there are a variety of ways to “delete”  
> associations between specific User\<-\>List combinations.
> 
> ActiveRecord lists delete, clear etc.
> 
> However, these methods all operate by setting foreign keys in the join table  
> to nil and do not remove rows from the join table.

Where did you get the idea that calling the delete method on a HABTM  
relationship doesn’t physically delete the row from the join table?  
That’s  
exactly what it does. In fact, I just verified it in one of my apps, to  
make  
certain.

> Q1: isn’t the join table going to contain a LOT of useless records with nil as  
> one column value after a period of time (after deleting many individual  
> User/List associations etc.)

Well, yes, it would, if that’s what delete and clear did, but they  
don’t, so I  
don’t think you need to worry about tons of nil, nil rows…

> Q2. is there a way to break an association between a specific User/List pair  
> by DELETING the row in the join table? (instead of setting a foreign key  
> column value to NIL)

What you need to do is set up a HABTM relation and do some adds and  
deletes, and  
watch the development log roll by on your terminal window. You will see  
the  
DELETE SQL calls as they happen.

-Brian
