Skip to content
Browse files

README - Add to your existing project

Added "how to do" for existing project.
  • Loading branch information...
1 parent cb522b5 commit db466d8aacf59b14d38c3685fd07a063bf3f08ad @evandrodp evandrodp committed with parndt
Showing with 44 additions and 0 deletions.
  1. +44 −0 README.md
View
44 README.md
@@ -151,6 +151,50 @@ class Category < ActiveRecord::Base
end
```
+
+## Add to your existing project
+
+To make use of `awesome_nested_set`, your model needs to have 3 fields:
+`lft`, `rgt`, and `parent_id`. The names of these fields are configurable.
+You can also have an optional field, `depth`.
+
+Create a migration to add fields:
+
+```ruby
+class AddNestedToCategories < ActiveRecord::Migration
+
+ def self.up
+ add_column :categories, :parent_id, :integer # Comment this line if your project already has this column
+ # Category.where(parent_id: 0).update_all(parent_id: nil) # Uncomment this line if your project already has :parent_id
+ add_column :categories, :lft , :integer
+ add_column :categories, :rgt , :integer
+ add_column :categories, :depth , :integer # this is optional.
+
+ # This is necessary to update :lft and :rgt columns
+ Category.rebuild!
+ end
+
+ def self.down
+ remove_column :categories, :parent_id
+ remove_column :categories, :lft
+ remove_column :categories, :rgt
+ remove_column :categories, :depth # this is optional.
+ end
+
+end
+```
+
+Enable the nested set functionality by declaring `acts_as_nested_set` on your model
+
+```ruby
+class Category < ActiveRecord::Base
+ acts_as_nested_set
+end
+```
+
+Your project is now ready to run with the `awesome_nested_set` gem!
+
+
## Conversion from other trees
Coming from acts_as_tree or another system where you only have a parent_id? No problem. Simply add the lft & rgt fields as above, and then run:

0 comments on commit db466d8

Please sign in to comment.
Something went wrong with that request. Please try again.