1
votes

I have an app with the following models: Categories; Sub-Categories and Products. I have the models set up so that: categories has_many sub_categories sub_categories belongs_to categories sub_categories has_many products products belongs_to sub_categories.

What I want is for each category to have individual sub_category(s), and each sub_category to have individual product(s). For example; I have a Category called "Foo", a sub_category called "Bar" and a product called "FooBar". I want "Foo" to only link to "Bar(s)" and "Bar" to only link to "FooBar(s)".

Thanks!

1

1 Answers

1
votes

You can use nested resources to accomplish this.

resources :categories do
  resources :sub_categories do
    resources :products
  end
end

Then you set the category and sub_category object using the parameters category_id and sub_category_id passed to the products controller.

The Rails guide on routing is awesome.