Skip to content
Harshal Bhakta edited this page Jul 31, 2019 · 1 revision

attr_accessible

If you're declaring acts_as_nested_set on a class that defines attr_accessible, you'll get a "Declare either attr_protected or attr_accessible" error unless you define the attr_accessible before acts_as_nested_set as follows:

class Category < ActiveRecord::Base
    attr_accessible :name, :description ...
    acts_as_nested_set
end

after_create

If you have an after_create :method that calls update! or save!, the depth is not set correctly. This happens because "depth" is set in the "after_save":https://github.com/collectiveidea/awesome_nested_set/blob/d4c1224d8bf6cabd5b0619ea1bdbdebabac9f3cb/lib/awesome_nested_set/awesome_nested_set.rb#L69 callback which gets called after the after_create callback.

Check this "gist":https://gist.github.com/harshalbhakta/1c46ef0c591d39c9ee489eac460fe337 to test the behaviour.

class Category < ActiveRecord::Base
  acts_as_nested_set

  # Don't use this
  after_create :update_slug

  # Use this
  after_create_commit :update_slug

  def update_slug
    self.update! slug: self.name.parameterize
  end
end