# Category Listing

**URL:** <https://www.ruby-forum.com/t/category-listing/141844>\
**Category:** Rails\
**Created:** [July 1, 2008, 12:20am UTC](https://www.ruby-forum.com/t/category-listing/141844 "2008-07-01T00:20:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Camel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@Camel](https://www.ruby-forum.com/u/Camel)\
**Post date:** [July 1, 2008, 12:20am UTC](https://www.ruby-forum.com/t/category-listing/141844/1 "2008-07-01T00:20:22Z")

</div>

Hi there.

I’m relatively new to Ruby on Rails, although not to programming in  
general.

I’m attempting to make a sort of classified ad system. I’d like to  
have a list of my categories with the number of ads in that category  
next to it. For example:

Jobs - Carpenters (1)  
Jobs - Electricians (10)  
Jobs - IT (3)

etc.

I’ve created model, Category, and I’ve linked it to the Classifieds  
model, using belongs\_to: and has\_many: tags.

My problem is that I’ve listed the categories and counts successfully,  
but they’re repeating. Instead of the above, it looks more like this:

Jobs - Carpenters (1)  
Jobs - Electricians (10)  
Jobs - IT (3)  
Jobs - Electricians (10)  
Jobs - Electricians (10)  
Jobs - IT (3)

It’s repeating because I’ve got the loop running in category.rb which  
is called from the category\_list.html.

Here’s the code from category.rb:

def self.count\_categories  
count(:id, :conditions =\> {:category\_id =\> i})  
end

My question is, how can I set it to display only the categories and  
their counts without repeating?

Thanks,  
Brian

---

<div class="post-metadata">

**Author:** ![Camel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@Camel](https://www.ruby-forum.com/u/Camel)\
**Post date:** [July 1, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/category-listing/141844/2 "2008-07-01T14:37:28Z")

</div>

i think your problem lies in the way you have arranged your relations.  
You talk about Classifieds (Advert i assume), Category and Tag. Now  
that is 2 namespaces for a single advert. Ie: One advert belongs to  
one category and has many tags…

If we leave the tags alone for the time being, and you want a category  
object holding many adverts and an advert belonging only to one  
category, then you would do something like:

===category.rb

class Category \< AR::Base  
has\_many :adverts  
end

===advert.rb

class Advert \< AR::Base  
belongs\_to :category  
end

your adverts table needs a column :category\_id, :integer

=== views/categories/index.rhtml

\<%- for category in @categories- %\>  
Jobs - \<%= category.name.upcase! %\> ( \<%= category.adverts.size  
%\> )  
\<%- end -%\>

=== controllers/categories\_controller.rb

def index  
@categories = Category.find(:all)  
end

that should do it.

For the tags system, you will need to set up a many to many relation  
9which can be of 2 types, has\_and\_belongs\_to\_many or  
has\_many :through, check this out if you’re unaware of them  
[has\_many :through - Many-to-many Dance-off!](http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off) )  
where an advert has many tags and a tag has many adverts. then you can  
do things like:

@tag.adverts  
@advert.tags

hope it helped

---

<div class="post-metadata">

**Author:** ![Camel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@Camel](https://www.ruby-forum.com/u/Camel)\
**Post date:** [July 1, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/category-listing/141844/3 "2008-07-01T17:27:17Z")

</div>

Thanks, that did exactly what I wanted!
