3

我有一个名为的模型Collection,其配置如下:

acts_as_nested_set scope: :account, dependent: :destroy
belongs_to :parent, class_name: 'Collection'

nested_set_options在我升级到 Rails 4.1.4(从 4.0.5 升级)之前,使用视图助手工作正常:

<%= f.input :parent_id, label: 'Parent Collection',
            collection: nested_set_options(@collections) { |collection|
              "#{'-' * collection.level} #{collection.name}"
            },
            include_blank: '-- No Parent --' %>

我得到这个例外:

undefined method `name' for nil:NilClass

/Users/cpeters/Sites/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb:45:in  `add_to_inverse_association'
/Users/cpeters/Sites/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb:37:in `block in associate_parents'
activerecord (4.1.4) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.1.4) lib/active_record/relation/delegation.rb:46:in `each'
/Users/cpeters/Sites/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb:31:in `associate_parents'
/Users/cpeters/Sites/plugins/awesome_nested_set/lib/awesome_nested_set/helper.rb:33:in `block in nested_set_options'
/Users/cpeters/Sites/plugins/awesome_nested_set/lib/awesome_nested_set/helper.rb:32:in `each'
/Users/cpeters/Sites/plugins/awesome_nested_set/lib/awesome_nested_set/helper.rb:32:in `nested_set_options'
app/views/admin/resources/collections/_form.html.erb:7:in `block in _app_views_admin_resources_collections__form_html_erb__1427458998633439544_70350945783440'

这是从 GitHub 运行的最新版本(提交f823ffb)。

4

1 回答 1

3

事实证明,我的belongs_to配置是不需要的并且是导致问题的原因。

我从模型中删除了这一行,我的所有测试都通过了,应用程序运行良好:

belongs_to :parent, class_name: 'Collection'

不确定我之前是否出于某种原因将其放在那里,但我觉得即使少一行代码也是一种胜利。

于 2014-07-15T14:22:07.187 回答