# Issues with saving associations and acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/issues-with-saving-associations-and-acts-as-tree/92783>\
**Category:** Rails\
**Created:** [April 24, 2007, 5:36am UTC](https://www.ruby-forum.com/t/issues-with-saving-associations-and-acts-as-tree/92783 "2007-04-24T05:36:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myron\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Myron\_M](https://www.ruby-forum.com/u/Myron_M)\
**Post date:** [April 24, 2007, 5:36am UTC](https://www.ruby-forum.com/t/issues-with-saving-associations-and-acts-as-tree/92783/1 "2007-04-24T05:36:25Z")

</div>

I’m having some issues with the saving of my models. I’ll explain my  
models, and then I’ll explain the problems I’m having. I’ve got a  
model called ContentPage that contains the html for several of the  
pages of my website. I’m trying to add a feature where a document  
outline, with links to the appropriate places in the page, is  
automatically generated when a ContentPage is saved. The document  
outline is generated based on the header tags. My document outline is  
held in my document\_outline\_item table and corresponding model.  
Here’s the migration I have that creates it:

create\_table :document\_outline\_items do |t|  
t.column “text”, :string, :null =\> false  
t.column “content\_page\_id”, :integer, :null =\> false  
t.column “parent\_id”, :integer, :null =\> true  
t.column “position”, :integer, :null =\> true  
t.column “anchor\_identifier”, :string, :null =\> false  
end

The content\_page\_id field is a foreign key to the content\_page this  
document outline item is for. The parent\_id field points back to a  
document\_outline\_item so that my doc outline can be nested.

Here’s how I’ve setup my document outline item model:

class DocumentOutlineItem \< ActiveRecord::Base  
belongs\_to :content\_page  
acts\_as\_tree :order =\> :position  
acts\_as\_list :scope =\> :parent\_id

validates\_presence\_of :text, :position, :content\_page\_id,  
:anchor\_identifier

ContentPage has the corresponding has\_many :document\_outline\_items  
declaration.

So, ContentPage has a document\_outline\_items collection. This contains  
all the document outline items for the content page, not just the top  
level ones. DocumentOutlineItem is setup to have a children  
document\_outline\_items collection using the acts\_as\_tree declaration.  
So, one document outline item can be held in both the ContentPage’s  
document\_outline\_items collection, and also in a parent  
DocumentOutlineItem’s children collection.

When I modify an existing content page, everything works beautifully.  
My document outline gets generated and saved properly. When I create  
a new content page, only the leaf document\_outline\_items (i.e. those  
that do not have any children) get saved. This occurs because the  
content\_page\_id has not been set on any of the document\_outline\_items,  
since the ContentPage has not yet been saved and hence does not have  
an id. At some point, the content\_page\_id gets set on the leaf items,  
and they get saved. On the parent items, the child items have not yet  
had this field set when they are validated, and hence the children  
association fails validations, and they do not get saved.

Any ideas on how to fix this? And when do the parent\_id and  
content\_page\_id get set in the children?

Thanks,  
Myron
