rails ancestry嵌套表单

map*_*ap7 5 tree nested-sets nested-forms ruby-on-rails-3

我刚刚开始使用Ancestry而不是awesome_nested_set,我想创建一个嵌套表单,以便我可以在一个表单中创建一个父帐户和许多子帐户.问题似乎是Ancestry不允许你为新的父母创建一个孩子.

在awesome_nested_set中,我可以在rails控制台中执行此操作

a = Account.new
a.children.build
Run Code Online (Sandbox Code Playgroud)

当我输入a.children时,即使我的帐户父项尚未创建,我也可以在那里看到新的孩子.这允许我显示一个包含父帐户和一些空白孩子的表单,然后在提交时我会忽略任何空白孩子并创建整个批次.

如果我尝试使用祖先做同样的事情,我会收到以下错误:

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>'
Run Code Online (Sandbox Code Playgroud)

这是祖先的限制吗?我有不同的方式可以使用祖先创建嵌套表单?

Tap*_*nen 5

今天遇到同样的问题 - 我认为这是对祖先限制,看着代码(它引发了这个错误if new_record?).

虽然它不理想,但目前我正在使用children.create而不是children.build.