Forum: Ruby on Rails has_and_belongs_to_many :self

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joshua M. (Guest)
on 2006-01-09 20:38
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
Carmen -. (Guest)
on 2006-01-09 20:53
> 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?


i think it would be good to add a new association type to ActiveRecord
called "friends_with" or something since it seems that this is a pretty
common case in the 'social networking' or 'semantic web' or whatever the
bloggers are calling it this week..

for just tricking habtm into being this type of association, you could
make the friendships bidirectional with callbacks, something like:

h_a_b_t_m :after_add => :create_reverse_association, :after_remove =>
:remove_reverse_association

 def create_reverse_association(user)
    user.buddy << self unless user.buddy.include?(self)
  end

  def remove_reverse_association(user)
    user.buddy.delete(self) if user.buddy.include?(self)
  end
Carmen -. (Guest)
on 2006-01-09 23:26
someone who replied off list said:
> i dont think you'll ever convince _____ to add social-networking or other 
domain-specific ...

right, "friends_with" was a poor choice of words. intra-class
bidirectional connections could be used for anything from 'node maps'
for a public transportation or computer network, to
friendster/myspace/xanga/etc, to 'see also' dictionary definitions or
wiki entries, product cross-sells, etc.

of course rails is already well suited to the task, but something like:

connections :name => 'buddy'

is a lot simpler than the half a dozen things you have to do now wrt
overriding the class name, adding callbacks, specifying foreign keys,
etc...
Joshua M. (Guest)
on 2006-01-11 14:53
carmen  wrote:
> someone who replied off list said:
>> i dont think you'll ever convince _____ to add social-networking or other 
domain-specific ...
>
> right, "friends_with" was a poor choice of words. intra-class
> bidirectional connections could be used for anything from 'node maps'
> for a public transportation or computer network, to
> friendster/myspace/xanga/etc, to 'see also' dictionary definitions or
> wiki entries, product cross-sells, etc.
>
> of course rails is already well suited to the task, but something like:
>
> connections :name => 'buddy'
>
> is a lot simpler than the half a dozen things you have to do now wrt
> overriding the class name, adding callbacks, specifying foreign keys,
> etc...

Thanks a lot for your replies. I tried it with another habtm and it
worked, as you suggested.
Now what about "connections :name => 'buddy'"? This doesn't really
exist, does it?

Btw. is there a way to be noticed when someone replies to my topic?
Bob M. (Guest)
on 2009-05-16 06:56
Joshua M. 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!
Joshua M. (Guest)
on 2009-05-16 22:02
> 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...
Bob M. (Guest)
on 2009-05-17 00:25
Joshua M. 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!
This topic is locked and can not be replied to.