Forum: Ruby on Rails Issues with saving associations and acts_as_tree

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
42d4590355a1404230fbc9aff4dd377b?d=identicon&s=25 Myron Marston (Guest)
on 2007-04-24 05:36
(Received via mailing list)
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

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,

ContentPage has the corresponding has_many :document_outline_items

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?

This topic is locked and can not be replied to.