has_and_belongs_to_many :self

Joshua Muheim wrote:

Hi all

I got the following class:

class Member < ActiveRecord::Base   has_and_belongs_to_many :buddies,                           :class_name => 'Member',                           :join_table => 'members_have_buddies',                           :foreign_key => 'member_id',                           :association_foreign_key => 'buddy_id' end

You can see that a member can have any other member as a "buddy"; these relationships are held in the table members_have_buddies (member_id, buddy_id).

So far, so good. I have a member "my_member", that has 3 other members marked as buddies, so I got 3 entries in the members_have_buddies table. When I delete my_member, then the 3 entries in the table are also deleted, as I expect it to do. But when I delete one of the 3 other members, that are marked as members, then the related entry in the members_have_buddies table does *not* get deleted!

I assume, this is an error, is it? Or have I done anything wrong? How can I solve this problem?

Thanks a lot for help. Josh

Hi Josh,

I know you posted this while back, but I'm having a great deal of trouble with my habtm self-referencing setup and was wondering if you could help. I have the exact same set up as what you showed in your original post.

The problem I run into is that it seems the DB only saves in one direction, so if I make memberA have buddies memberB & memberC, when I call memberA.friends I get both memberB & memberC returned. However if I do memberB.friends I get no return. Same thing for memberC.friends.

And since the db only saves uni-directionally, it's not letting me assign the other way around and make memberA a buddy under memberB or C.

Any suggestions on how to address this and make it so I can store and retrieve accurate friend connections?

Thanks!

Any suggestions on how to address this and make it so I can store and retrieve accurate friend connections?

Thanks!

Hi there

I'm not at my computer right now and will try to respond some time later, but maybe you should try to really save the models and force a reload on the already loaded ones...? Don't know if this could cause the problem...

Joshua Muheim wrote:

Any suggestions on how to address this and make it so I can store and retrieve accurate friend connections?

Thanks!

Hi there

I'm not at my computer right now and will try to respond some time later, but maybe you should try to really save the models and force a reload on the already loaded ones...? Don't know if this could cause the problem...

Thanks Joshua,

I've searched around a bit and it seems that other people have run into this same issue. I've added some extra sql into my model now which will write the opposite direction record into the join table explicitly any time a relationship is made. So 1.friends(2) now leads to a join table of:

1 | 2 2 | 1

Whereas before it would just be:

1 | 2

This seems to solve the problem for me, but it feels a bit hacky. I'm curious as to whether or not you had to do something extra like this to get the joins saving bi-directionally?

I have a different issue now as well, only relationships to new objects are being saved. So:

    member1.buddies[0] = member2     member1.save

Doesn't record anything to the join table. However:

    member1.buddies[0] = Member.new     member1.save

saves the relationship to the join table correctly. The discrepency seems totally strange to me, any clues on that problem?

Thanks!