# Routing and has\_many

**URL:** <https://www.ruby-forum.com/t/routing-and-has-many/174499>\
**Category:** Rails\
**Created:** [August 27, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/routing-and-has-many/174499 "2009-08-27T18:13:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hansfh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3e96dc/32.png) [@hansfh](https://www.ruby-forum.com/u/hansfh)\
**Post date:** [August 27, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/routing-and-has-many/174499/1 "2009-08-27T18:13:56Z")

</div>

Hi,  
im a rails newbie, so please be gentle:

let’s say category habtm products and i’ve got a product with the id  
7.

when i use the url

/categories/7/products

should the server show me only the categories of product 7?

On my Server it always shows all categories i have.

How do i solve this the right way?

I edited the routes.rb so, that

map.resources :categories, :has\_many =\> [:products]

But this does not change anythings, using  
/categories/7/products always leads to the index action of the  
products controller.

Is the right way to test in the index action of the products  
controller whether category\_id is set in params and use this  
information to only show products of this category? Or is there a  
better way?

Thanks,

HansFH

---

<div class="post-metadata">

**Author:** ![hansfh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3e96dc/32.png) [@hansfh](https://www.ruby-forum.com/u/hansfh)\
**Post date:** [August 27, 2009, 6:48pm UTC](https://www.ruby-forum.com/t/routing-and-has-many/174499/2 "2009-08-27T18:48:29Z")

</div>

On Aug 27, 2009, at 8:54 AM, hansfh wrote:

> should the server show me only the categories of product 7?  
> /categories/7/products always leads to the index action of the  
> products controller.
> 
> Is the right way to test in the index action of the products  
> controller whether category\_id is set in params and use this  
> information to only show products of this category? Or is there a  
> better way?
> 
> Thanks,
> 
> HansFH

If you have a line in ProductsController#index like:

@products = Product.find(:all)

Then of course you’ll see all products. The question is what you want  
to see at that url. I’d expect to see all the products in category 7

def index  
if find\_category  
@products = @category.products  
else  
flash[:error] = “No category with id=#{params[:category\_id]}”  
redirect\_to categories\_path  
end  
end  
def find\_category  
@category = Category.find\_by\_id(params[:category\_id])  
end

And you can even put the find\_category into a before\_filter and  
redirect from it directly.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![hansfh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3e96dc/32.png) [@hansfh](https://www.ruby-forum.com/u/hansfh)\
**Post date:** [August 27, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/routing-and-has-many/174499/3 "2009-08-27T21:39:58Z")

</div>

2009/8/27 hansfh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> But this does not change anythings, using  
> /categories/7/products always leads to the index action of the  
> products controller.
> 
> Is the right way to test in the index action of the products  
> controller whether category\_id is set in params and use this  
> information to only show products of this category? Or is there a  
> better way?

I am not sure if this answers your question or not, and you may know  
this already, but if you have found the product with id 7 and it is in  
variable @product say, then the categories for that product are  
available as an array in @product.categories.

Apologies if this is nothing to do with what you want to achieve.

Colin

---

<div class="post-metadata">

**Author:** ![hansfh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3e96dc/32.png) [@hansfh](https://www.ruby-forum.com/u/hansfh)\
**Post date:** [August 29, 2009, 5:06am UTC](https://www.ruby-forum.com/t/routing-and-has-many/174499/4 "2009-08-29T05:06:44Z")

</div>

It sounds like your routes are set up alright, but yeah, you’ll still  
need to add code so your action knows how to handle it.

In your ProductsController#index action, you probably have something  
like this?

@products = Product.all

Since you have category\_id in the params, something like this should  
work:

if params[:category\_id]  
@category = Category.find(params[:category\_id])  
@products = @category.products  
end

Personally, if I have a controller that’s always used as a nested  
resource, I put the initialization of the parent into a before\_filter,  
so it’s called for every action.
