Noob question on deleting from join tables


#1

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)


#2

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