# Find sub sub records

**URL:** <https://www.ruby-forum.com/t/find-sub-sub-records/177587>\
**Category:** Rails\
**Created:** [October 20, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/find-sub-sub-records/177587 "2009-10-20T17:16:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [October 20, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/find-sub-sub-records/177587/1 "2009-10-20T17:16:26Z")

</div>

Hi everyone, I have the following problem, maybe someone can help me  
figure out what to do.

To simplify my situation, I’m going to make an example of what I need,  
this is not the real app structure.

Let’s suppose we have three models, Library, Author, Book.

Manager  
has\_and\_belongs\_to\_many :area\_managers

AreaManager  
has\_and\_belongs\_to\_many :managers  
has\_many :employees

Employee  
belongs\_to :area\_manager

Given a manager, I’d like to find all the employees under its area  
managers in a single query.

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [October 20, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/find-sub-sub-records/177587/2 "2009-10-20T17:41:24Z")

</div>

2009/10/20 Serafino P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> has\_and\_belongs\_to\_many :area\_managers  
> I think this will work, though I am not quite sure given this a habtm  
> rather than has\_many. Include here  
> has\_many :employees, :through =\> :area\_managers

> managers in a single query.  
> @employees = @manager.employees

Colin

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [October 21, 2009, 7:42am UTC](https://www.ruby-forum.com/t/find-sub-sub-records/177587/3 "2009-10-21T07:42:43Z")

</div>

Hi friend

u can try this one…There are three tables :Library,Author,book

Class Library \<ActiveRecord::Base  
has\_many :Authors  
has\_many :Books  
end

Class Author \<ActiveRecord::Base  
belongs\_to :library  
has\_many :books  
end

Class Book \<ActiveRecord::Base  
belongs\_to :library  
belongs\_to :author  
end

On Tue, Oct 20, 2009 at 8:46 PM, Serafino P. \<
