How to refactor to use 'acts_as_tree'?

I am trying to refactor my code to use 'acts_as_tree'.

I have a self-referencing model called Property (<ActiveRecord) which
currently has the usual belongs_to and has_many declarations:

belongs_to :collection,
             :class_name => "Property",
             :foreign_key => "collection_id"
  has_many :collected_properties,
             :class_name => "Property",
             :foreign_key => "collection_id"

I'd like to replace these with an 'acts_as_tree'.

In my controller 'create' method currently I have the following code:

   # create a new Property of the correct sub class
   @property = Property.factory(params[:which_type], params[:property])
    # if adding a child to a root - not always the case!
    if @flash[:is_collection] then
      # this finds the immediate root (root id stored in flash)
      # this updates the foreign key collection_id in my new child row
      @property.collection = root
    # save the new row
      flash[:notice] = 'Property was successfully created.'
      redirect_to :action => 'list'
      render(:partial =>

How would I refactor the @propery.collection = root and
lines to make use of 'acts_as_tree'. I note that in the Agile Web Dev
with Rails book that you can write:

root.children.create(...) but not sure how to change this to make use
of the save() and my instance variable @property


fyi, I added 'acts_as_tree' and just changed '@property.collection =
root' to '@property.parent = root' - works fine I expect benefits when
writing the code to render the list as I can use 'children'/'parent'
etc methods now :slight_smile:

Suggestions for improvement still welcome of course.