# Need help understanding how to sort and view items through categories

**URL:** <https://www.ruby-forum.com/t/need-help-understanding-how-to-sort-and-view-items-through-categories/183921>\
**Category:** Rails\
**Created:** [February 22, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/need-help-understanding-how-to-sort-and-view-items-through-categories/183921 "2010-02-22T18:30:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roflkk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@roflkk](https://www.ruby-forum.com/u/roflkk)\
**Post date:** [February 22, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/need-help-understanding-how-to-sort-and-view-items-through-categories/183921/1 "2010-02-22T18:30:24Z")

</div>

This is probably an easy guess for some, but after searching the web  
for quite some time, I’ve concluded that I need help understanding  
this.

My case:

I’m creating an ad application where people can sell and buy items  
through their own ads. An ad belongs to one category, and therefore  
the question: When a user views all the current ads as a list  
(webisteurl/ads) - how do I make the categories show up next to that  
list like a clickable sorting menu?

Help would be very much appreciated!

---

<div class="post-metadata">

**Author:** ![roflkk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@roflkk](https://www.ruby-forum.com/u/roflkk)\
**Post date:** [February 22, 2010, 10:22pm UTC](https://www.ruby-forum.com/t/need-help-understanding-how-to-sort-and-view-items-through-categories/183921/2 "2010-02-22T22:22:49Z")

</div>

> (webisteurl/ads) - how do I make the categories show up next to that  
> list like a clickable sorting menu?

I’m not 100% sure which part you’re stuck on. To get the association  
set up  
you need to look in to ActiveRecord Relationships, in particular  
has\_and\_belongs\_to\_many.

class Advert \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories  
end

Then in the view do something along the lines of:

…\<%= @advert.content %\>…

 \<% for category in @advert.categories %\> - \<%= link\_to category.name, category\_path(category) %\>
 \<% end %\>

You’d have a category controller; maybe using restful routes or a named  
route such as:

map.category “/category/:id”, :controller =\> “categories”, :action =\>  
“show”

In that controller you’d do:

@articles.find\_by\_category(params[:id])

And iterate over those articles in the view.

Depending on where you were stuck, hopefully that’s given you some hints  
to  
get you moving. If not, post back with more details.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![roflkk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@roflkk](https://www.ruby-forum.com/u/roflkk)\
**Post date:** [February 24, 2010, 11:40pm UTC](https://www.ruby-forum.com/t/need-help-understanding-how-to-sort-and-view-items-through-categories/183921/3 "2010-02-24T23:40:02Z")

</div>

It helped quite a bit actually. Thanks!
