# Self-referential habtm: why are my keys null?

**URL:** <https://www.ruby-forum.com/t/self-referential-habtm-why-are-my-keys-null/117961>\
**Category:** Rails\
**Created:** [October 22, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-why-are-my-keys-null/117961 "2007-10-22T20:43:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [October 22, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-why-are-my-keys-null/117961/1 "2007-10-22T20:43:36Z")

</div>

I’m trying to set up a directional self-referential HABTM to represent  
an arbitrary semi-hierarchical structure; it works for any data  
prepopulated into the db, but when I try to create a new relationship,  
I just get NULLs in both key columns (or 0’s if I turn on the no-null  
constraint).

Here are the migrations:

class CreateNodes \< ActiveRecord::Migration  
def self.up  
create\_table :nodes do |t|  
t.column :name, :string  
end  
end

def self.down  
drop\_table :nodes  
end  
end

class CreateNodesNodes \< ActiveRecord::Migration  
def self.up  
create\_table :nodes\_nodes, :id =\> false do |t|  
t.column :parent\_id, :integer  
t.column :child\_id, :integer  
end  
end

def self.down  
drop\_table :nodes\_nodes  
end  
end

And here is the model:

class Node \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :parents, :foreign\_key =\> :child\_id,  
:association\_foreign\_key  
=\> :parent\_id,  
:class\_name =\> ‘Node’;  
has\_and\_belongs\_to\_many :children, :foreign\_key =\> :parent\_id,  
:association\_foreign\_key  
=\> :child\_id,  
:class\_name =\> ‘Node’;  
end

I’ve also tried swapping the foreign and association\_foreign keys  
around.

Any idea why this isn’t working? I’m using Rails 1.2.5 on MySQL, and  
aside from the above code and the database.yml I haven’t touched  
anything that “rails app” created.

> ruby script/console  
> Loading development environment.
> 
> > p = Node.new  
> > =\> #\<Node:0xb7192348 @new\_record=true, @attributes={“name”=\>nil}\>
> 
> > p.name = ‘parent’  
> > =\> “parent”
> 
> > c = Node.new  
> > =\> #\<Node:0xb714a8f4 @new\_record=true, @attributes={“name”=\>nil}\>
> 
> > c.name = ‘child’  
> > =\> “child”\>\> p.children \<\< c  
> > =\> [#\<Node:0xb714a8f4 @new\_record=true,  
> > @attributes={“name”=\>“child”}\>]
> 
> > p.children  
> > =\> [#\<Node:0xb714a8f4 @new\_record=true,  
> > @attributes={“name”=\>“child”}\>]
> 
> > c.parents  
> > =\> []
> 
> > p.save  
> > =\> true
> 
> > c.save  
> > =\> true
> 
> > p.reload  
> > =\> #\<Node:0xb7192348 @new\_record\_before\_save=true, @children=nil,  
> > @new\_record=false, @attributes={“name”=\>“parent”, “id”=\>“1”},  
> > @parents=nil, @errors=#\<ActiveRecord::Errors:0xb71251a8 @errors={},  
> > @base=#\<Node:0xb7192348 …\>\>\>
> 
> > p.children  
> > =\> []
> 
> > c.reload  
> > =\> #\<Node:0xb714a8f4 @children=nil, @new\_record\_before\_save=false,  
> > @new\_record=false, @attributes={“name”=\>“child”, “id”=\>“2”},  
> > @parents=nil, @errors=#\<ActiveRecord::Errors:0xb7124f64 @errors={},  
> > @base=#\<Node:0xb714a8f4 …\>\>\>
> 
> > c.parents  
> > =\> []

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [October 22, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-why-are-my-keys-null/117961/2 "2007-10-22T21:41:01Z")

</div>

Aw, of all the . . . have to specify the keys in the association as  
strings, not symbols.

There should maybe be some normalization going on there, since it’s  
not always obvious (to me, anwyay) when symbols work and when they  
don’t…
