# Finding Listings that Belong to Categories

**URL:** <https://www.ruby-forum.com/t/finding-listings-that-belong-to-categories/149144>\
**Category:** Rails\
**Created:** [September 21, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/finding-listings-that-belong-to-categories/149144 "2008-09-21T17:26:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![recreant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@recreant](https://www.ruby-forum.com/u/recreant)\
**Post date:** [September 21, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/finding-listings-that-belong-to-categories/149144/1 "2008-09-21T17:26:31Z")

</div>

I have two models that are playing nicely together. The first is the  
Category model that uses acts\_as\_tree to create a hierarchy of  
categories based on parent\_id. Example:

Category 1  
Sub 1  
Sub 2  
Subsub 1  
Subsubsub 1  
Subsub 2  
Sub 3  
Category 2  
Category 3

Etc…

The second is a Listings model that has a many-to-many relationship to  
the Categories model using has\_many :through

So here’s my question: How would I go about finding all of the  
listings in “Sub 2” AND below?

Doing a find for the listings that belong to Sub 2 is obviously very  
easy, but what about including subsub 1, subsubsub1, and subsub 2 and  
returning all tof the results in one instance object (like  
@listings)?

Any general idea of how to go about this would be helpful. I’m pretty  
new at this so I hope i’m clear enough in my explanation. Thanks!

---

<div class="post-metadata">

**Author:** ![recreant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@recreant](https://www.ruby-forum.com/u/recreant)\
**Post date:** [September 22, 2008, 10:10am UTC](https://www.ruby-forum.com/t/finding-listings-that-belong-to-categories/149144/2 "2008-09-22T10:10:45Z")

</div>

recreant wrote:

> Sub 3
> 
> Doing a find for the listings that belong to Sub 2 is obviously very  
> easy, but what about including subsub 1, subsubsub1, and subsub 2 and  
> returning all tof the results in one instance object (like  
> @listings)?
> 
> Any general idea of how to go about this would be helpful. I’m pretty  
> new at this so I hope i’m clear enough in my explanation. Thanks!

You should add nested-set capability to the model:  
[GitHub - chris/better\_nested\_set: better\_nested\_set Rails plugin (my fork from official SVN)](http://github.com/chris/better_nested_set/tree/master)  
[GitHub - collectiveidea/awesome\_nested\_set: An awesome replacement for acts\_as\_nested\_set and better\_nested\_set.](http://github.com/collectiveidea/awesome_nested_set/tree/master)

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
