# Recursvie Search Refactoring

**URL:** <https://www.ruby-forum.com/t/recursvie-search-refactoring/147800>\
**Category:** Rails\
**Created:** [September 5, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/recursvie-search-refactoring/147800 "2008-09-05T15:44:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 5, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/recursvie-search-refactoring/147800/1 "2008-09-05T15:44:56Z")

</div>

Hi, All

I was wondering if somebody could suggest a better way to do it. I got  
it working but it’s a bit dirty and returns too much extra fluff.

I’m trying to do a recursive search on an a collection of Objects.

A better way to illustrate it is an Org Chart.

Lets say I have Employee and ManagedByEmployee Objects

Employee hold user id , name, address, etc…  
also:  
has\_many :managed\_by\_employees, :dependent =\> :destroy

ManagedByEmpoyee holds 3 attributes: id, employee\_id and  
managed\_by\_employee\_id  
also:  
belongs\_to :employee  
belongs\_to :managed\_by\_employee, :class\_name =\> “Employee”, :foreign\_key  
=\> “managed\_by\_employee\_id”

This allows me to do any searches 1 level deep. I can find who this  
particular employee manages and who he is managed by. That trivial.

What I want to do is a drill down search.

Let’s say I do:  
@emps = find\_all\_by\_managed\_by\_employee\_id(3)  
This would return me all of the empployees that are managed by employee  
#3, lets say I want to go through that list and check all of the  
employees and see who they manage, and so on and so forth - until I  
reach the bottom, where I find employee that don’t manage anyone.

Then I want a similar function that goes in the other direction.

I wrote a model methods that handle that = but there are 2 problems with  
them:

1. They return too many duplicate records - and I have to clean them up  
at the end.
2. I return all of the employees that employee # 3 manages but, I can’t  
tell how they relate to employee 3 (well I can go through the chain  
again and figure it out) But there’s got to be a btter way to do it then  
running more sql statement.

Below are my model functions

def self.find\_employees\_that\_i\_manage(employee, employees\_array)  
arr = employees\_array  
arr += @managed\_employees =  
find\_all\_by\_managed\_by\_employee\_id(employee)  
unless @managed\_employees.empty?  
@managed\_employees.each do |s|  
arr += find\_employees\_that\_i\_manage(s.employee\_id, arr)  
end  
end  
arr |= arr  
end

def self.find\_employees\_that\_manage\_me(employee, employees\_array)  
arr = employees\_array  
arr += @managed\_by\_employees = find\_all\_by\_employee\_id(employee)  
unless @managed\_by\_employees.empty?  
@managed\_by\_employees.each do |s|  
arr += find\_employees\_that\_manage\_me(s.managed\_by\_employee\_id,  
arr)  
end  
end  
arr |= arr  
end

Here is how I call them from controller:  
@manages\_employees =  
managedByemployee.find\_employees\_that\_i\_manage(params[:id], p=[])  
@managed\_by\_employees =  
managedByemployee.find\_employees\_that\_manage\_me(params[:id], p=[])

Anyone has any suggestions?
