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)? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This earlier thread might be helpful. http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/20ad808815a17080/b27fdea7e816df82 Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 01.09.2008, at 23:29, Craig Demyanovich wrote:> This earlier thread might be helpful. > > http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/20ad808815a17080/b27fdea7e816df82Thanks, 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, thanks, maybe i''ll get something intresting or write it myself ) On 02.09.2008, at 0:55, Craig Demyanovich wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 02.09.2008, at 3:14, Craig Demyanovich wrote:> 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?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So you like the result but not the name of the association? On Tue, Sep 2, 2008 at 4:10 AM, Valery Kvon <addager-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > > On 02.09.2008, at 3:14, Craig Demyanovich wrote: > > > 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? > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That''s right. On 02.09.2008, at 15:47, Craig Demyanovich wrote:> So you like the result but not the name of the association? > > On Tue, Sep 2, 2008 at 4:10 AM, Valery Kvon <addager-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Not exactly: association methods differ from each other (.friends > and .reverse_friends): > >> friend1.friends > => friend2 > > >> friend2.reverse_friends > => friend1 >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Tue, Sep 2, 2008 at 10:32 AM, Valery Kvon <addager-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That''s right. > On 02.09.2008, at 15:47, Craig Demyanovich wrote: > > So you like the result but not the name of the association? > > On Tue, Sep 2, 2008 at 4:10 AM, Valery Kvon <addager-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> Not exactly: association methods differ from each other (.friends >> and .reverse_friends): >> >> friend1.friends >> => friend2 >> >> >> friend2.reverse_friends >> => friend1 >> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---