# Should I use iterations to display sub categories?

**URL:** <https://www.ruby-forum.com/t/should-i-use-iterations-to-display-sub-categories/131265>\
**Category:** Rails\
**Created:** [March 7, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/should-i-use-iterations-to-display-sub-categories/131265 "2008-03-07T14:26:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 7, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/should-i-use-iterations-to-display-sub-categories/131265/1 "2008-03-07T14:26:32Z")

</div>

Hello,

I have a categories table that has an id and a category\_id so that I can  
have a :belongs\_to on itself.

On the left of my website I would like to display the main categories,  
then their sub categories below them.

I have read in the book I have that iterations may be a possibility.

I thought that perhaps there are other ways to do this such has having  
in my controller have two instance variables, one would have categories  
where category\_id is null to display top categories, then the other  
variable would would have categories where category\_id = a certain id.

Of course I just thought that the above mentioned might not work because  
what about if I added a top category, then I would have to hard code  
something each time - not desirable.

So what are your suggestions after readin my babble?

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 7, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/should-i-use-iterations-to-display-sub-categories/131265/2 "2008-03-07T14:33:59Z")

</div>

Joel S. wrote:

> Hello,
> 
> I have a categories table that has an id and a category\_id so that I can  
> have a :belongs\_to on itself.
> 
> On the left of my website I would like to display the main categories,  
> then their sub categories below them.
> 
> I have read in the book I have that iterations may be a possibility.

so far, so good. that’s the best way to do it

> I thought that perhaps there are other ways to do this such has having  
> in my controller have two instance variables, one would have categories  
> where category\_id is null to display top categories, then the other  
> variable would would have categories where category\_id = a certain id.

you would need more instance vars. one for each sub-category. since most  
likely you don’t know in advance how many that would be…

> Of course I just thought that the above mentioned might not work because  
> what about if I added a top category, then I would have to hard code  
> something each time - not desirable.
> 
> So what are your suggestions after readin my babble?

you first load all cats with category\_id 0, then iterate over children  
and so on. if necessary, in a recursive function but it’s more likely,  
that you’ll know the deepness of nested cats, than their amount.  
still easier, than making guesses about the number of categories and  
needed variables

---

<div class="post-metadata">

**Author:** ![capslock118](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/df705f/32.png) [@capslock118](https://www.ruby-forum.com/u/capslock118)\
**Post date:** [March 8, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/should-i-use-iterations-to-display-sub-categories/131265/3 "2008-03-08T20:45:36Z")

</div>

You might want to look at acts\_as\_tree (now a plugin) – it does what  
you’re asking.

With your current design you might consider this:

class Category \< ARec:Base

has\_many :subcategories, :class\_name=\>‘Category’,  
:foreign\_key=\>‘category\_id’  
…  
end

You’ll gain some db efficiency since you can do your initial fetch as:

@categories =  
Category.find(:all, :conditions=\>{:category\_id=\>nil},  
:include=\>:subcategories)

That will fetch all the ‘main’ categories and their immediate  
subcategories.

On Mar 7, 8:26 am, Joel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
