# Working with topics and categories

**URL:** <https://www.ruby-forum.com/t/working-with-topics-and-categories/61834>\
**Category:** Rails\
**Created:** [May 28, 2006, 3:47pm UTC](https://www.ruby-forum.com/t/working-with-topics-and-categories/61834 "2006-05-28T15:47:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 28, 2006, 3:47pm UTC](https://www.ruby-forum.com/t/working-with-topics-and-categories/61834/1 "2006-05-28T15:47:52Z")

</div>

Hello guys,

# I’m a newbie in RoR and I have a problem. My DB structure is

topics table : id(int,11)  
name(varchar,255)

# categories : id (int,11) topic\_id(varchar,255) name(varchar,255)

–Models:–  
class Topic \< ActiveRecord::Base  
has\_many :categories  
end  
class Category \< ActiveRecord::Base  
belongs\_to :topic  
end

I need page structure like this:

### Topic1
 (id = 1)
- Category1
 (topic\_id = 1) - Category2
 (topic\_id = 1) - Category3
 (topic\_id = 1) - Category4

 (topic\_id = 1) 
### Topic2
 (id = 2)
- Category1
 (topic\_id = 2) - Category2
 (topic\_id = 2) - Category3
 (topic\_id = 2) - Category4

 (topic\_id = 2) 
### Topic3
 (id = 3)
- Category1
 (topic\_id = 3) - Category2
 (topic\_id = 3) - Category3
 (topic\_id = 3) - Category4

 (topic\_id = 3)

… and so on.

How can I do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 28, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/working-with-topics-and-categories/61834/2 "2006-05-28T17:34:35Z")

</div>

@topic = Topic.find(:all, :include =\> “categories”) in your controller

then in your view

\<% @topic.each do |t| %\>

### \<%= t.name %\>

 \<% t.categories.each do |c| %\> - \<%= c.name %\>
 \<% end %\> 
 \<% end %\>

adam

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 28, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/working-with-topics-and-categories/61834/3 "2006-05-28T17:57:44Z")

</div>

Thanks Adam. Your answer is very helpful.  
I have a question again. I have 3 columns, and 9 topics with N cats.

Topic 1 Topic 4 Topic 7  
Cat 1 Cat 1 Cat 1  
Cat 2 Cat 2 Cat 2  
Cat 3 Cat 3 Cat 3  
Topic 2 Topic 5 Topic 8  
Cat 1 Cat 1 Cat 1  
Cat 2 Cat 2 Cat 2  
Cat 3 Cat 3 Cat 3  
Topic 3 Topic 6 Topic 9  
Cat 1 Cat 1 Cat 1  
Cat 2 Cat 2 Cat 2  
Cat 3 Cat 3 Cat 3

How can I get 1st, 2nd and 3rd; 4th, 5th and 6th4 7th, 8th and 9th  
topics from the loop?  
Thanks again!

---

<div class="post-metadata">

**Author:** ![blinchik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blinchik](https://www.ruby-forum.com/u/blinchik)\
**Post date:** [May 29, 2006, 5:45am UTC](https://www.ruby-forum.com/t/working-with-topics-and-categories/61834/4 "2006-05-29T05:45:17Z")

</div>

well you can use tables and every 3 rows insert a new 1 column table.

Or you can access the topic number order by doing

t.categories[1] , t.categories[2] and so on instead of  
t.categories.each

adam
