# Act\_as\_tree, belongs\_to and has\_many foreign keys incorrect

**URL:** <https://www.ruby-forum.com/t/act-as-tree-belongs-to-and-has-many-foreign-keys-incorrect/108649>\
**Category:** Rails\
**Created:** [July 5, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/act-as-tree-belongs-to-and-has-many-foreign-keys-incorrect/108649 "2007-07-05T17:22:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [July 5, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/act-as-tree-belongs-to-and-has-many-foreign-keys-incorrect/108649/1 "2007-07-05T17:22:37Z")

</div>

Two models:

class Project \<\< ActiveRecord::Base  
has\_many :files  
end

class File \<\< ActiveRecord::Base  
has\_one :project  
acts\_as\_tree  
end

Inside of the project model, when creating a new product, I want to  
store the hierarchy of files within it.

If I do,

my\_top\_level\_file = my\_project\_object.files.create(:file\_name =\>  
‘foo’);  
my\_top\_level\_file.children.create(:file\_name =\> ‘bar’)

When the data gets saved, the first row in the Files table gets the  
new product\_id value that is also inserted into the Projects table,  
but the child row does not, giving me results like:

Projects:  
id: 1  
name: A\_Name

Files:  
id: 1  
project\_id: 1  
parent\_id: null  
filename: foo

id: 2  
product\_id: 0 \<---------------------- Should this not be 1?  
parent\_id: 1  
filename: bar

Firstly, am I doing things right? If so, is this a bug, or is this  
how things were intended to work? Surely all rows in the files table  
should have the correct foreign key attached to them?

Thanks,

Stephen.
