Eli Gibson
2008-Nov-20 07:03 UTC
Any progress on undirected self-referential many to many relationships?
I am going to re-ask a question that has been asked a few times in the past. What is the best way to represent an undirected self-referential many to many relationship. Trivial examples of this include an edge in an undirected graph, or a mutual friendship. Alice is a person Bob is a person Alice and Bob are friends of each other We have a whole bunch of extra information about their friendship (when it started, who introduced them, where they met, etc.) We want to query "Are Bob and Alice friends?", "Who are Bob''s friends?", "Who are the people that introduce Bob to friends?" etc. We could represent this as a has_many :through relationship, with a friendship_information database (containing extra information), and a friendship database with two entries in the database per friendship (<Bob, Alice> and <Alice, Bob>). But that seems inefficient. Is there a better way to do this? Alternatively, is there a way to merge 2 lists into a single has_many? Like this: has_many :initiated_friendships, :foreign_key =>''befriender_id'', :class_name => ''Friendship'', :dependent => :destroy has_many :received_friendships, :foreign_key =>''befriendee_id'', :class_name => ''Friendship'', :dependent => :destroy has_many :friends, :through=>union (:received_friendships,:initiated_friendships) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ewout Kleinsmann
2008-Nov-27 23:18 UTC
Re: Any progress on undirected self-referential many to many relationships?
Bump! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phlip
2008-Nov-28 01:41 UTC
Re: Any progress on undirected self-referential many to many relationships?
Ewout Kleinsmann wrote:> Bump!Unbump! Just use the has_many :through option, and build the intermediate table by hand. There''s nothing "inefficient" about it; you just have to keep tuning the Model objects until they express your business rules, like any other model relationship... -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Blake M.
2011-Apr-15 21:41 UTC
Re: Any progress on undirected self-referential many to many relationships?
There is something inefficient about it. For it to act like an undirected graph the join table needs to have 2 rows per edge. AFAIK there is no good solution to this out there ... a "good" solution meaning one which doesn''t double the size of the join table to make the directed graph behave like an undirected graph. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Seemingly Similar Threads
- Self-Referential Many-To-Many relationships where the relationship itself has data.
- Undirected to Directed Graph ( Clustering Based on Link Strength)
- Self-referential has_many :through relationship
- acts_as_state_machine override :initial state
- How to access attribute in a self-referential many-to-many relationship