Skip to content

Commit

Permalink
Let's assume users don't have existing parent_id column, for now.
Browse files Browse the repository at this point in the history
I'll add a section on migrating to Arboreal later.
  • Loading branch information
mdub committed Apr 18, 2010
1 parent e593fb9 commit b1d606d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.rdoc
Expand Up @@ -10,33 +10,34 @@ filtering/pagination can be performed.
It delegates as much work as possible to the underlying DBMS, making it efficient to:

* fetch all ancestors, descendants or siblings of a node
* prevent loops in the hierarchy
* move nodes (or subtrees) around
* prevent loops
* rebuild the hierarchy

== Getting started

First, install the "arboreal" gem, and add it to your Rails project's <tt>config/environment.rb</tt>.

Next, you'll need a migration to add an +ancestry_string+ column, and index thereupon:
Next, you'll need a migration to add +parent_id+ and +ancestry_string+ columns, and indices:

class MakeThingsArboreal < ActiveRecord::Migration

def self.up
add_column "things", "parent_id", :integer
add_index "things", ["parent_id"]
add_column "things", "ancestry_string", :string
add_index "things", ["ancestry_string"]
Thing.rebuild_ancestry
end

def self.down
remove_index "things", ["ancestry_string"]
remove_column "things", "ancestry_string"
remove_index "things", ["parent_id"]
remove_column "things", "parent_id"
end

end

This assumes that the table concerned already has a +parent_id+ column; if not, add that too (type +integer+, with a supporting index).

Finally, you can declare your model arboreal:

class Thing < ActiveRecord::Base
Expand All @@ -46,7 +47,7 @@ Finally, you can declare your model arboreal:
# .. etc etc ...

end

== Navigating the tree

Arboreal adds the basic relationships you'd expect:
Expand Down

0 comments on commit b1d606d

Please sign in to comment.