# Representing a graph

**URL:** <https://www.ruby-forum.com/t/representing-a-graph/79678>\
**Category:** Rails\
**Created:** [December 18, 2006, 2:16am UTC](https://www.ruby-forum.com/t/representing-a-graph/79678 "2006-12-18T02:16:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hapciu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ce7236/32.png) [@hapciu](https://www.ruby-forum.com/u/hapciu)\
**Post date:** [December 18, 2006, 2:16am UTC](https://www.ruby-forum.com/t/representing-a-graph/79678/1 "2006-12-18T02:16:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hapciu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ce7236/32.png) [@hapciu](https://www.ruby-forum.com/u/hapciu)\
**Post date:** [December 18, 2006, 3:44am UTC](https://www.ruby-forum.com/t/representing-a-graph/79678/2 "2006-12-18T03:44:00Z")

</div>

Self referential has\_many :through, with the segment as the join model?

-faisal

---

<div class="post-metadata">

**Author:** ![hapciu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ce7236/32.png) [@hapciu](https://www.ruby-forum.com/u/hapciu)\
**Post date:** [December 18, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/representing-a-graph/79678/3 "2006-12-18T15:03:12Z")

</div>

On Dec 17, 8:16 pm, hapciu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![hapciu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ce7236/32.png) [@hapciu](https://www.ruby-forum.com/u/hapciu)\
**Post date:** [December 18, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/representing-a-graph/79678/4 "2006-12-18T16:21:30Z")

</div>

Hi,

On Dec 17, 8:16 pm, hapciu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 M.

> **[Édition et distribution de livres sur la musique, de partitions et de revues...](https://symetrie.com/fr)**
>
> Présentation des publications de Symétrie en musique, musicologie et médecine de la voix.

Better Nested Set for rails:  
[http://opensource.symetrie.com/trac/better\_nested\_set](http://opensource.symetrie.com/trac/better_nested_set)
