# Self-referential join model does not work

**URL:** <https://www.ruby-forum.com/t/self-referential-join-model-does-not-work/54903>\
**Category:** Rails\
**Created:** [March 15, 2006, 4:11am UTC](https://www.ruby-forum.com/t/self-referential-join-model-does-not-work/54903 "2006-03-15T04:11:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcora](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ec9cab/32.png) [@marcora](https://www.ruby-forum.com/u/marcora)\
**Post date:** [March 15, 2006, 4:11am UTC](https://www.ruby-forum.com/t/self-referential-join-model-does-not-work/54903/1 "2006-03-15T04:11:36Z")

</div>

I have been trying to model a labeled graph using ActiveRecord (trunk  
version). Basically, I have a ‘Node’ model which represents a node  
linking  
to other nodes. Links (aka edges) are labeled (i.e., have a ‘label’  
attribute and other behavior). Thus I decided to model edges as an  
‘Edge’  
model and use a ‘has\_many :trough’ association in the Node model.  
However,  
it just doesn’t work, regardless of how hard I’ve tried.

The database structure is as follows:

class InitialSchema \< ActiveRecord::Migration  
def self.up  
create\_table :nodes do |t|  
t.column :title, :string, :null =\> false  
t.column :body, :text, :null =\> false, :default =\> ‘’  
end  
create\_table :edges do |t|  
t.column :node\_id, :integer, :null =\> false  
t.column :related\_node\_id, :integer, :null =\> false  
t.column :label, :string, :null =\> false, :default =\> ‘’  
end  
add\_index :edges, [:node\_id, :related\_node\_id], :unique =\> true  
end  
def self.down  
drop\_table :edges  
drop\_table :nodes  
end  
end

The two models are as follows:

class Node \< ActiveRecord::Base  
validates\_presence\_of :title  
has\_many :edges, :dependent =\> true  
has\_many :related\_nodes, :through =\> :edges, :class\_name =\>  
‘Node’, :foreign\_key =\> ‘node\_id’  
end

class Edge \< ActiveRecord::Base  
belongs\_to :node  
belongs\_to :related\_node, :class\_name =\> ‘Node’, :foreign\_key =\>  
‘related\_node\_id’  
validates\_uniqueness\_of :related\_node\_id, :scope =\> :node\_id  
validates\_presence\_of :node, :related\_node  
end

In script/console I then create two nodes and link them with an edge as  
follows:

> > from\_node = Node.create :title =\> ‘From’  
> > to\_node = Node.create :title =\> ‘To’  
> > e = Edge.create :node =\> from\_node, :related\_node =\> to\_node  
> > from\_node.edges.find\_first.related\_node  
> > =\> #\<Node:0xb7430ee4 @attributes={“body”=\>"", “title”=\>“To”, “id”=\>“2”}\>

SO FAR SO GOOD! but if I ask the ‘from\_node’ for ‘related\_nodes’ this is  
what I get back!!!

> > from\_node.edges.related\_nodes.find\_first  
> > =\> #\<Node:0xb742e20c @attributes={“body”=\>"", “title”=\>“From”,  
> > “id”=\>“1”}\>

Same result even if I change :foreign\_key in the Node model from  
‘node\_id’  
to ‘related\_node\_id’. Furthermore, :association\_foreign\_key is not  
accepted  
as a parameter to has\_many :through associations.

Does anybody have a clue of what I am doing wrong here? Or is it that  
Rails  
1.1 does not support self-referential join models?

Thanx in advance for the help and consideration,

Edoardo “Dado” Marcora

---

<div class="post-metadata">

**Author:** ![marcora](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ec9cab/32.png) [@marcora](https://www.ruby-forum.com/u/marcora)\
**Post date:** [March 17, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/self-referential-join-model-does-not-work/54903/2 "2006-03-17T23:19:00Z")

</div>

See if the patch at [http://dev.rubyonrails.org/ticket/4289](http://dev.rubyonrails.org/ticket/4289) works for  
you. It adds the :association\_foreign\_key to has\_many.

---

<div class="post-metadata">

**Author:** ![marcora](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ec9cab/32.png) [@marcora](https://www.ruby-forum.com/u/marcora)\
**Post date:** [March 20, 2006, 6:17am UTC](https://www.ruby-forum.com/t/self-referential-join-model-does-not-work/54903/3 "2006-03-20T06:17:58Z")

</div>

Thanx for adding the ticket and for coming up with the patch. I am sure  
things will be resolved quickly in rails/trunk.

Lee M. wrote:

> See if the patch at [http://dev.rubyonrails.org/ticket/4289](http://dev.rubyonrails.org/ticket/4289) works for  
> you. It adds the :association\_foreign\_key to has\_many.

---

<div class="post-metadata">

**Author:** ![marcora](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ec9cab/32.png) [@marcora](https://www.ruby-forum.com/u/marcora)\
**Post date:** [March 29, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/self-referential-join-model-does-not-work/54903/4 "2006-03-29T18:47:21Z")

</div>

Did you get this sorted?

I got something that works for me using the lovely :through. I copied  
it onto a blogger account since it would have been real messy here…

> **[Henster: Self Referential many-many using :through](http://rubbishshoes.blogspot.com/2006/03/self-referential-many-many-using.html)**
>
> Henster

beers  
-h
