gpt4 book ai didi

ruby-on-rails - AwesomeNestedSet + Rails 4.1.4 `nested_set_options` 抛出异常

转载 作者:行者123 更新时间:2023-12-04 06:18:30 24 4
gpt4 key购买 nike

我有一个名为 Collection 的模型,它是这样配置的:

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

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

<%= 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)。

最佳答案

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

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

belongs_to :parent, class_name: 'Collection'

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

关于ruby-on-rails - AwesomeNestedSet + Rails 4.1.4 `nested_set_options` 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24760721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com