5

I've just started using Ancestry instead of awesome_nested_set and I would like to create a nested form so that I can create a parent account and many children accounts all in the one form. The problem seems to be that Ancestry doesn't allow you to create a child for a new parent.

In awesome_nested_set I could do this in the rails console

a = Account.new
a.children.build

When I type a.children I can see that new child in there even though my account parent hasn't been created yet. This allowed me to display a form with the parent account and a few blank children, then on submit I would just ignore any blank children and create the whole lot.

If I try and do the same using ancestry I get the following error:

a=Account.new
a.children
Ancestry::AncestryException: No child ancestry for new record. Save record before performing tree operations.
    from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:62:in `child_ancestry'
    from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:132:in `child_conditions'
    from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:136:in `children'
    from (irb):8
    from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
    from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
    from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Is this a limitation of ancestry? I there a different way in which I could create a nested form using ancestry?

map7
  • 5,096
  • 6
  • 65
  • 128

1 Answers1

6

Ran into the same problem today - I believe it to be a limitation of ancestry, looking at the code (it raises this error if new_record?).

While it's not ideal, at the moment I'm using children.create instead of children.build.

Chris Frederick
  • 5,482
  • 3
  • 36
  • 44
Tapio Saarinen
  • 2,499
  • 3
  • 20
  • 18
  • Yes it's not ideal. Due to this problem and also the fact that it doesn't play nicely with thinking-sphinx and pagination over a multi-dimensional hash looks impossible I'm returning to awesome-nested-set. I do like the cleanness of ancestry and it would be good for simple things, but it's not for my current project. – map7 Jun 17 '11 at 04:16
  • 1
    I've run into the same problem trying to write tests with FactoryGirl. Sorry to zombie this question, but I thought this might be interesting to some. – IAmNaN Apr 26 '12 at 23:12