# Parent listing children

**URL:** <https://www.ruby-forum.com/t/parent-listing-children/95497>\
**Category:** Rails\
**Created:** [June 3, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/parent-listing-children/95497 "2006-06-03T23:58:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 3, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/parent-listing-children/95497/1 "2006-06-03T23:58:09Z")

</div>

My database is set up as Categories \> Things associated by category\_id  
and has\_many and belongs\_to.

How would I go about listing all the categories and under each Category  
is it’s children? Is there an easy rails way to do this - preferably  
without using acts as tree? Any help is appreciated - thanks!

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 4, 2006, 12:35am UTC](https://www.ruby-forum.com/t/parent-listing-children/95497/2 "2006-06-04T00:35:39Z")

</div>

Hello Adrian,

> My database is set up as Categories \> Things associated by  
> category\_id and has\_many and belongs\_to.
> 
> How would I go about listing all the categories and under each  
> Category is it’s children? Is there an easy rails way to do this -  
> preferably  
> without using acts as tree? Any help is appreciated - thanks!

Can you describe your models and what you want to achieve  
more clearly ?

From what I understand, you’ve got Category has\_many :things  
and Thing belongs\_to :category .

So you can retrieve your categories and preload their children  
with :

@categories = Category.find :all, :include =\> :things

# in the view, iterate on categories and each time, use category.things

Or when you’re speaking of acts\_as\_tree, do you imply  
that Category is a self-referencing model (has\_many :subcategories,  
:class\_name =\> ‘Category’, :foreign\_key =\> ‘parent\_id’ …) ?  
and you want to display subcategories ?

– Jean-FranÃ§ois.

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [June 4, 2006, 1:11am UTC](https://www.ruby-forum.com/t/parent-listing-children/95497/3 "2006-06-04T01:11:59Z")

</div>

Basically I have two tables, Categories and Things with the model  
explained above I need it to view such as

+Category1  
-Thing1  
-Thing2  
+Category2  
-Thing#  
-Thing#etc.

The only way i see it being done is having a for loop displaying  
categories and then another for loop within that displaying the things  
in reference to the category. I was curious if there was a rails way to  
do this as there seems to be alot that I’m missing and I would also have  
to call SQL within the view which would go against MVC abit.

Using your suggestion that actually sounds good as far I know - how  
would i go about implementing grouped by to make it work?

Thanks for your reply!
