Single Table HABTM

33 views
Skip to first unread message

Valery Kvon

unread,
Sep 1, 2008, 12:09:31 PM9/1/08
to rubyonra...@googlegroups.com
Hi, all

I'm looking for a trick how to get something like that:

obj1 = SmthClass.new
obj2 = SmthClass.new

obj1.connections << obj2

>> obj1.connections
=> obj2

>> obj2.connections
=> obj1

??

PS: The first solve is to make the third join model, and put them all
necessary has_many + has_many relationship, then give the join model
optional method using transaction etc, so it's inevitable to have two
identical strings in the join models' database table (to obj1 and
obj2)...? Or i can write some handler to seek obj1 and obj2 using
'their own' foreign keys one by one through each other (two has_many
methods with two different foreign keys)?

Craig Demyanovich

unread,
Sep 1, 2008, 3:29:45 PM9/1/08
to rubyonra...@googlegroups.com

Valery Kvon

unread,
Sep 1, 2008, 4:25:10 PM9/1/08
to rubyonra...@googlegroups.com

On 01.09.2008, at 23:29, Craig Demyanovich wrote:


Thanks, I saw it!  But unfortunately this method makes only "one-way" association and not vice versa (if i'm not mistaken).
Exactly I need - vice versa associations. I thought the only way to do it is to have two strings in the database (obj1 to obj2 and obj2 to obj1). Where is the trick?

Craig Demyanovich

unread,
Sep 1, 2008, 4:55:53 PM9/1/08
to rubyonra...@googlegroups.com
Ah. I haven't done that yet. A Google search for

"many-to-many" "self-referential" rails

turns up some hits that could be useful.

Regards,
Craig

Valery Kvon

unread,
Sep 1, 2008, 5:00:58 PM9/1/08
to rubyonra...@googlegroups.com
Ok, thanks, maybe i'll get something intresting or write it myself )

Valery Kvon

unread,
Sep 1, 2008, 5:16:39 PM9/1/08
to rubyonra...@googlegroups.com
This is similar: http://hideto.javaeye.com/blog/75887

What are you think? I guess, there is no transaction improvements to
be a super nice :)

On 02.09.2008, at 0:55, Craig Demyanovich wrote:

Craig Demyanovich

unread,
Sep 1, 2008, 7:14:18 PM9/1/08
to rubyonra...@googlegroups.com
Actually, according to a reply in another thread, you could build on what I shared earlier. First, declare the habtm as described before, then declare another habtm named as you want and with the foreign_key and association_foreign_key swapped around.

class User < ActiveRecord::Base
  has_and_belongs_to_many :friends,
    :class_name => "User",
    :join_table => "users_friends",
    :foreign_key => "user_id",
    :association_foreign_key => "friend_id"
  has_and_belongs_to_many :reverse_friends,
    :class_name => "User",
    :join_table => "users_friends",
    :foreign_key => "friend_id",
    :association_foreign_key => "user_id"
end

Does that work?

Valery Kvon

unread,
Sep 2, 2008, 4:10:04 AM9/2/08
to rubyonra...@googlegroups.com
Not exactly: association methods differ from each other (.friends
and .reverse_friends):
>> friend1.friends
=> friend2

>> friend2.reverse_friends
=> friend1

that is not what i want

Craig Demyanovich

unread,
Sep 2, 2008, 7:47:18 AM9/2/08
to rubyonra...@googlegroups.com
So you like the result but not the name of the association?

Valery Kvon

unread,
Sep 2, 2008, 10:32:55 AM9/2/08
to rubyonra...@googlegroups.com
That's right.

Craig Demyanovich

unread,
Sep 2, 2008, 10:47:00 AM9/2/08
to rubyonra...@googlegroups.com
I doubt that you can have two habtm associations with the same name, but I've never tried it. I suggest you try it. If it works, you have what you want. If it doesn't work, I suggest finding unique names that you like for each association. Either way, please share your findings.

Regards,
Craig
Reply all
Reply to author
Forward
0 new messages