Created: 2008-06-04 22:53
Updated: 2017-05-11 05:50
License: mit



A nested set is a datastruture in a database, sort of like a tree, but unlike a tree it allows you to find all descendants of a node with a single query. Loading a deeply nested structure with nested sets is therefore a lot more efficient than using a tree. So what's the disadvantage? Nested sets are a lot harder to maintain, since inserting and moving records requires management and it is easy to corrupt the dataset. Enter: EvenBetterNestedSet. Amount of micromanaging you need to do: 0. EvenBetterNestedSet does it all for you.



[sudo] gem install eb_nested_set


[sudo] gem install jnicklas-eb_nested_set --source http://gems.github.com

From source:

git clone git://github.com/jnicklas/eb_nested_set.git
cd eb_nested_set
rake install

If you're running Rails, just add it to your environment.rb file

config.gem 'eb_nested_set'

You can also install it as a Rails plugin.

script/plugin install git://github.com/jnicklas/eb_nested_set.git


EvenBetterNestedSet is hosted at GitHub. If you'd like to contribute, please create a fork and send pull requests :)

Declaring nested sets

This is how you declare a nested set:

class Directory < ActiveRecord::Base



The directories table should have the columns 'parent_id', 'left' and 'right'.

Now just set the parent to wherever you want your node to be located and EvenBetterNestedSet will do the rest for you.

d = Directory.new

d.children.create!(:name => 'blah')
d.children.create!(:name => 'gurr')
d.children.create!(:name => 'doh')

d.bounds #=> 1..8
d.children[1].bounds #=> 4..5
d.children[1].name #=> 'gurr'
d.children[1].parent #=> d

c = Directory.create!(:name => 'test', :parent => d.directory[1]

Finding with nested sets

EvenBetterNestedSet will not automatically cache children for you, because it assumes that this is not always the preferred behaviour. If you want to cache children to a nested set, just do:

d = Directory.find(42)

or more conveniently:

d = Directory.find_with_nested_set(42)


Add these keys to your translation file:

  parent_not_in_scope: "nay, thy parent not be in scope {{scope_name}}"
  illegal_nesting: "arr tis be illegal nesting"


Is available at Rubyforge.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more