I am representing a graph structrure in my DB. I have a table for nodes (which holds node names for now) and a table for segments/links. Each segment has the id-s of its 2 end-nodes. How do i specify this in the Rails model for a Segment ? I can''t say belongs_to twice ... Is there a better way to do this ? Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
Self referential has_many :through, with the segment as the join model? -faisal On Dec 17, 2006, at 8:16 PM, hapciu wrote:> I am representing a graph structrure in my DB. I have a table for > nodes > (which holds node names for now) and a table for segments/links. Each > segment has the id-s of its 2 end-nodes. > How do i specify this in the Rails model for a Segment ? I can''t say > belongs_to twice ... > Is there a better way to do this ? > > Thanks > > -- > 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 -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-18 14:01 UTC
Re: representing a graph
On Dec 17, 8:16 pm, hapciu <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I am representing a graph structrure in my DB. I have a table for nodes > (which holds node names for now) and a table for segments/links. Each > segment has the id-s of its 2 end-nodes. > How do i specify this in the Rails model for a Segment ? I can''t say > belongs_to twice ... > Is there a better way to do this ?Something along these lines? This creates a directed graph, you would just add two segments for each node pair with the source/target reversed if you want an undirected graph. ## Models: class Node < ActiveRecord::Base has_many :segments has_many :nodes, :through => :segments, :source => :target end class Segment < ActiveRecord::Base belongs_to :source, :class_name => "Node" belongs_to :target, :class_name => "Node" end ## Migrations create_table "nodes" do |t| t.column "name", :string end create_table "segments" do |t| t.column "source_id", :integer t.column "target_id", :integer end Best, -r> > Thanks > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Hi, On Dec 17, 8:16 pm, hapciu <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:>> I am representing a graph structrure in my DB. I have a table for >> nodes >> (which holds node names for now) and a table for segments/links. Each >> segment has the id-s of its 2 end-nodes. >> How do i specify this in the Rails model for a Segment ? I can''t say >> belongs_to twice ... >> Is there a better way to do this ?Mybe you ''d like to have a look on nestedset theory and our implementation as plugin. Thanks to some devs that helped a lot, our plugin has many tests and begins to work well. Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---