Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails ancestry nested form

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?

like image 699
map7 Avatar asked Jun 09 '11 00:06

map7


1 Answers

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.

like image 50
Tapio Saarinen Avatar answered Sep 22 '22 04:09

Tapio Saarinen