0

My table is:

|  1 | Electronics     | Cameras         
|  2 | Electronics     | Computers       
|  3 | Electronics     | Mobile Phone    
|  4 | Electronics     | Sound & Vision  
|  5 | Clothes         | womens clothing 
|  7 | Clothes         | women shoes     
|  8 | Clothes         | women hand bags 
|  9 | Clothes         | Mens clothing   
| 10 | Clothes         | Kids clothing  

I'm trying to get a link display on my index page like this:

**Electronics**
Computers
Mobile Phone 
Sound & Vision
**Clothes**
womens clothing
women shoes 
Mens clothing 

How do i do this?

My model is like this:

class Category < ActiveRecord::Base
  has_many :products
   attr_accessible :division,:subdivision
  end 


class Product < ActiveRecord::Base
        has_many :photos,:dependent => :destroy
        has_many :line_items, :through => :product_id
        belongs_to :merchant
        belongs_to  :category
        attr_accessor :quantity
        accepts_nested_attributes_for :photos, :line_items

Category table is the parent and product has a category_id. I want to display categories in the above manner, only if there are any products and my product model looks like this:

Once links display when I click on the link, I'm planning to show all the related products.

Can anyone guide me how to display the categories which have products?

2
  • This is a view issue. Can you show us what your view/controller files look like? Commented Mar 25, 2013 at 2:05
  • I did not create a view yet.But in my category controller looks something like this and I see the data in my show.html.erb .But I do not know how to display it on my home page index.html.erb pageclass CategoryController < ApplicationController def index @category = Category.all end Commented Mar 25, 2013 at 2:08

1 Answer 1

1

You can do this easily with Enumerable#group_by

<% Category.all.group_by{|c| c.division }.each do |division, subdivisions| %>
  <%= "**#{division}**" %><br>
  <% subdivisions.each do |subdivision| %>
    <%= subdivsion %><br>
  <% end %>
<% end %>

http://railscasts.com/episodes/29-group-by-month?view=asciicast

The block you pass to group_by gets evaluated, and every record that evaluates to the same value gets grouped together under a hash key, that you can loop over with .each

Sign up to request clarification or add additional context in comments.

2 Comments

You may want to add an explanation of what the code is doing, as opposed to just having a code-only answer. As-is, it was flagged as Low Quality (for review), even though it probably gives the OP a solution.
+1@LynnCrumbling. SO wants an explanation why the code works, not just code. It's the difference between giving someone a fish, or teaching them how to fish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.