3

I have a model called Collection that is configured as such:

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

Using the nested_set_options view helper worked fine until I upgraded to Rails 4.1.4 (up from 4.0.5):

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

I get this exception:

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'

This is running the latest from GitHub (commit f823ffb).

Chris Peters
  • 17,918
  • 6
  • 49
  • 65

1 Answers1

3

As it turns out, my belongs_to configuration was not needed and was causing the problem.

I removed this line from the model, and all of my tests are passing, and the app works fine:

belongs_to :parent, class_name: 'Collection'

Not sure if I put that in there for a reason earlier, but I feel that having even one line less of code is a victory.

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
  • It was there because, maybe, awesome_nested_set older config asked to put it there. Now, the current commit, has that line inside the method acts_as_nested_set_relate_parent! https://github.com/collectiveidea/awesome_nested_set/blob/08d522ad02ad6c0fff922fef9e96ae7a210a1b56/lib/awesome_nested_set/awesome_nested_set.rb (line 94) – Luis Ortega Araneda Jul 21 '14 at 20:00
  • 1
    @LuisOrtegaAraneda Thanks for pointing that out. I was curious! – Chris Peters Jul 21 '14 at 20:35