# Searching an acts\_as\_tree model

**URL:** <https://www.ruby-forum.com/t/searching-an-acts-as-tree-model/116798>\
**Category:** Rails\
**Created:** [October 9, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/searching-an-acts-as-tree-model/116798 "2007-10-09T22:00:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [October 9, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/searching-an-acts-as-tree-model/116798/1 "2007-10-09T22:00:40Z")

</div>

If I have a Person model that acts\_as\_tree, how can I find a  
particular person by drilling down through the family tree?

e.g.

for this family:

John  
| |  
Amy Dave  
| |  
Bob Bob

The urls

/john/amy/bob and /john/dave/bob

should find a different Person object. At the moment, I am doing it  
like this:

```
  people = (params[:people])
  people.each do |person|
    if person == people.first
      @person = Page.find_by_name(people.first)
    else
      @person = @person.children.find_by_name(person
    end
  end

```

This hits the database quite a few times though. Is there a better way  
to drill down to find the people?

DAZ

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [October 10, 2007, 10:26am UTC](https://www.ruby-forum.com/t/searching-an-acts-as-tree-model/116798/2 "2007-10-10T10:26:15Z")

</div>

Look at nested sets. ActiveRecord::Acts::NestedSet::ClassMethods  
With nested Sets it’s very simple to select the whole tree in one query.  
Disadvantage of nested sets are the huge amount of operations if you  
insert or update a tree node. But if you have small tree’s it’s the best  
solution.

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [October 10, 2007, 11:07am UTC](https://www.ruby-forum.com/t/searching-an-acts-as-tree-model/116798/3 "2007-10-10T11:07:09Z")

</div>

[http://agilewebdevelopment.com/plugins/betternestedset](http://agilewebdevelopment.com/plugins/betternestedset)
