# Newbie: Display hierarchical Records in a view

**URL:** <https://www.ruby-forum.com/t/newbie-display-hierarchical-records-in-a-view/99491>\
**Category:** Rails\
**Created:** [July 23, 2006, 12:57pm UTC](https://www.ruby-forum.com/t/newbie-display-hierarchical-records-in-a-view/99491 "2006-07-23T12:57:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mingworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mingworld](https://www.ruby-forum.com/u/mingworld)\
**Post date:** [July 23, 2006, 12:57pm UTC](https://www.ruby-forum.com/t/newbie-display-hierarchical-records-in-a-view/99491/1 "2006-07-23T12:57:29Z")

</div>

Hi,

I have two models: category and subcategory. (one to many  
relationship), and a controller ‘home’

I’d like to display all the categories and their sub categories in the  
view: home\index.rhtml.

I have a method in the ‘home’ controller’ like this:  
def list\_categories  
@categories = Category.find(:all)  
end

This list all the categories on a view, but I don’t know how to get  
all the sub categories that belong to their parent category. I’d like  
to display them like this in home\index.rhtml:

Category 2

- subCategory 1
- subCategory 2

Category 2

- subCategory 3
- subCategory 4

Could anybody help me on this one? Thanks in advance!

–  
Best regards,

Ming Ma

---

<div class="post-metadata">

**Author:** ![mingworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mingworld](https://www.ruby-forum.com/u/mingworld)\
**Post date:** [July 23, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/newbie-display-hierarchical-records-in-a-view/99491/2 "2006-07-23T18:40:43Z")

</div>

Ming Ma wrote:

> Hi,
> 
> I have two models: category and subcategory. (one to many  
> relationship), and a controller ‘home’
> 
> I’d like to display all the categories and their sub categories in the  
> view: home\index.rhtml.
> 
> I have a method in the ‘home’ controller’ like this:  
> def list\_categories  
> @categories = Category.find(:all)  
> end
> 
> This list all the categories on a view, but I don’t know how to get  
> all the sub categories that belong to their parent category. I’d like  
> to display them like this in home\index.rhtml:
> 
> Category 2
> 
> - subCategory 1
> - subCategory 2
> 
> Category 2
> 
> - subCategory 3
> - subCategory 4
> 
> Could anybody help me on this one? Thanks in advance!
> 
> –  
> Best regards,
> 
> Ming Ma

Ming,

You can access the subcategories directly from the category.

something like

for subcategory in @category.subcategories …

should give you what you are looking for.

Regards,

Michael

mmodica at cox dot net

---

<div class="post-metadata">

**Author:** ![mingworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mingworld](https://www.ruby-forum.com/u/mingworld)\
**Post date:** [July 23, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/newbie-display-hierarchical-records-in-a-view/99491/3 "2006-07-23T18:55:11Z")

</div>

On 23-Jul-06, at 6:54 AM, Ming Ma wrote:

> I’d like to display all the categories and their sub categories in the  
> view: home\index.rhtml.

You can access a category’s children through the category itself.  
Pretend we’re on the console:

Category.find(:first).subcategories

The above will return an array of all the subcategory objects for  
the :first category.

So, in home/index.rhtml

\<% @categories.each do |category| %\>  
\<%= category.name %\>  
\<% category.subcategories.each do |subcategory| %\>  
\<%= subcategory.name %\>  
\<% end %\>  
\<% end %\>

HTH

/Jeff

–  
[http://re.visioni.st](http://re.visioni.st)  
[http://quotedprintable.com](http://quotedprintable.com)

---

<div class="post-metadata">

**Author:** ![mingworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mingworld](https://www.ruby-forum.com/u/mingworld)\
**Post date:** [July 24, 2006, 1:26am UTC](https://www.ruby-forum.com/t/newbie-display-hierarchical-records-in-a-view/99491/4 "2006-07-24T01:26:45Z")

</div>

Thank you very much guys!

On 7/24/06, Jeffrey H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the :first category.  
> HTH
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Best regards,

Ming Ma
