0

I am new in RoR.

The problem is, I created fully functional product categorization with Ancesrty. But now I want to be able to retrieve products that is under these subcategories.

This is my categories show controller

@category = Category.find(params[:id])

Here is categories#show view.

 <b>Name of the category:</b>
<%= @category.name %>
<div class="product"
</div>
</p>

<% unless @category.children.empty? %>


       <ul id="sub-menu"> 
        <% @category.children.each do |sub1| %>
           <%= link_to (sub1.name), sub1 %>
<%end%>
<%end%>

It all works fine. but now I want to add in view categories/show function that shows all products that is under that category.

I added such code. In category/show controller

@cat_id = @category.id
@product = Product.where("category_id = ?",@cat_id)

In the categories show view I added

 <td><%= @product.name %></td>

Then clicking on some subcategory where should appear few products, there just shows up Product

To check if the code is right I put in the console. There it works fine and retrieve products related to this category.

I dont understand why then code not working in webserver when I launch application ?

Could it be because of some erorr in Associations ?

Thanks !

2
  • Wow , in Categories controller under action show at the end of the line I added .last Then it works and show product related to that category. But that shows just one. After I added .all It shows up error like undefined method `name' for #<Array:0x563d678>
    – Edgars
    Commented Mar 11, 2013 at 10:46
  • what could couse this kind off error, because I need all products related to that category. not just one :(
    – Edgars
    Commented Mar 11, 2013 at 10:49

4 Answers 4

1

in your controller, a more readable way is to use the plural form to indicate that you are expecting more than 1 object

@products = Product.where("category_id = ?", @cat_id)

Then in the view, just loop through these products

<% @products.each do |product| %>
  <%= product.name %>
<% end %>
0
1
@product = Product.where("category_id = ?",@cat_id)

will return an array if there are any products. So you will need to loop through the array.

<% @product.each do |product| %>
  <%= product.name %>
<% end %>
1
  • No problem. The arrows to the left of the answer will up vote and there might be a tick under them to accept.
    – Sam
    Commented Mar 11, 2013 at 11:06
1

I accept both of the answers, But I want to suggest to use Active Record Association for this type of problems. This makes your solution easier.

1

If you want to fetch only one product, you can use the find_by_ helper method of the model:

@product = Product.find_by_category_id(@cat_id)

With this it will fetch the first matching product which has category_id equal to @cat_id.

If you want to fetch all the products which belong to a category, you need to fetch all the products as others suggested:

@products = Product.where(:category_id => @cat_id)

And then in the view:

<% @products.each do |product| %>
  <%= product.name %>
<% end -%>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.