You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+45-6Lines changed: 45 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ for a description of different tree storage algorithms.
58
58
59
59
Note that closure_tree only supports ActiveRecord 4.1 and later, and has test coverage for MySQL, PostgreSQL, and SQLite.
60
60
61
-
1. Add `gem 'closure_tree'` to your Gemfile
61
+
1. Add `gem 'closure_tree'` to your Gemfile
62
62
63
63
2. Run `bundle install`
64
64
@@ -75,7 +75,7 @@ Note that closure_tree only supports ActiveRecord 4.1 and later, and has test co
75
75
```
76
76
77
77
Make sure you check out the [large number options](#available-options) that `has_closure_tree` accepts.
78
-
78
+
79
79
**IMPORTANT:Make sure you add `has_closure_tree`_after_`attr_accessible`and
80
80
`self.table_name =` lines in your model.**
81
81
@@ -115,9 +115,9 @@ NOTE: Run `rails g closure_tree:config` to create an initializer with extra
115
115
116
116
## Warning
117
117
118
-
As stated above, using multiple hierarchy gems (like `ancestry` or `nested set`) on the same model
118
+
As stated above, using multiple hierarchy gems (like `ancestry` or `nested set`) on the same model
119
119
will most likely result in pain, suffering, hair loss, tooth decay, heel-related ailments, and gingivitis.
120
-
Assume things will break.
120
+
Assume things will break.
121
121
122
122
## Usage
123
123
@@ -169,7 +169,7 @@ child1.ancestry_path
169
169
170
170
You can `find` as well as `find_or_create` by "ancestry paths".
171
171
172
-
If you provide an array of strings to these methods, they reference the `name` column in your
172
+
If you provide an array of strings to these methods, they reference the `name` column in your
173
173
model, which can be overridden with the `:name_column` option provided to `has_closure_tree`.
174
174
175
175
```ruby
@@ -256,6 +256,45 @@ server may not be happy trying to do this.
256
256
257
257
HT: [ancestry](https://github.com/stefankroes/ancestry#arrangement) and [elhoyos](https://github.com/mceachen/closure_tree/issues/11)
258
258
259
+
### Eager loading
260
+
261
+
Since most of closure_tree's methods (e.g. `children`) return regular `ActiveRecord` scopes, you can use the `includes` method for eager loading, e.g.
262
+
263
+
```ruby
264
+
comment.children.includes(:author)
265
+
```
266
+
267
+
However, note that the above approach only eager loads the requested associations for the immediate children of `comment`. If you want to walk through the entire tree, you may still end up making many queries and loading duplicate copies of objects.
268
+
269
+
In some cases, a viable alternative is the following:
270
+
271
+
```ruby
272
+
comment.self_and_descendants.includes(:author)
273
+
```
274
+
275
+
This would load authors for`comment`and all its descendants in a constant number of queries. However, the return value is an array of `Comment`s, and the tree structure is thus lost, which makes it difficult to walk the tree using elegant recursive algorithms.
276
+
277
+
A third option is to use `has_closure_tree_root` on the model that is composed by the closure_tree model (e.g. a `Post` may be composed by a tree of `Comment`s). Soin`post.rb`, you would do:
278
+
279
+
```ruby
280
+
# app/models/post.rb
281
+
has_closure_tree_root :root_comment
282
+
```
283
+
284
+
This gives you a plain `has_one` association (`root_comment`) to the root `Comment` (i.e. that with null `parent_id`).
285
+
286
+
It also gives you a method called `root_comment_including_tree`, which you can invoke as follows:
287
+
288
+
```ruby
289
+
a_post.root_comment_including_tree(:author)
290
+
```
291
+
292
+
The result of this call will be the root `Comment` with all descendants _and_ associations loaded in a constant number of queries. Inverse associations are also setup on all nodes, so as you walk the tree, calling `children`or`parent` on any node will _not_ trigger any further queries and no duplicate copies of objects are loaded into memory.
293
+
294
+
Theclass and foreign key of `root_comment` are assumed to be `Comment`and`post_id`, respectively. These can be overridden in the usual way.
295
+
296
+
The same caveat stated above with `hash_tree` also applies here: this method will load the entire tree into memory. If the tree is very large, this may be a bad idea, in which caseusing the eager loading methods above may be preferred.
297
+
259
298
### Graph visualization
260
299
261
300
```to_dot_digraph``` is suitable for passing into [Graphviz](http://www.graphviz.org/).
@@ -473,7 +512,7 @@ Yup! [Ilya Bodrov](https://github.com/bodrovis) wrote [Nested Comments with Rail
473
512
474
513
### Can I update parentage with `update_attribute`?
475
514
476
-
**No.**`update_attribute` skips the validation hook that is required for maintaining the
515
+
**No.**`update_attribute` skips the validation hook that is required for maintaining the
477
516
hierarchy table.
478
517
479
518
### Can I assign a parent to multiple children with ```#update_all```?
0 commit comments