9
9
9karamba2019-04-02 18:28:42
Ruby on Rails
9karamba, 2019-04-02 18:28:42

How to display products of all subcategories of a category?

Let's say I have a category "technology", and inside it subcategories ("computers", "phones", etc.). I want to display products from all subcategories of the 2nd category in localhost:3000/categories/2 .
Here is what I wrote for one subcategory, but how to display all products of all subcategories?

def show
  category = Category.find_by(id: params[:id])
    	subcategories = category.subcategories.first
    	@products= subcategory.products
end

The only thing that comes to mind is using each to add to the array and then output it, but there may be another way

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question