I have these relationships
User
has_many :products
has_many :stores
Product
belongs_to :user
belongs_to :store
belongs_to :category
Store
belongs_to :user
has_many :products
Category
acts_as_nested_set
has_many :products
In the homepage( view file ) i have a category drop down similar to amazon's:
<ul id="site-category-dropdown">
<li class="has-dropdown">
<a href="#">
<span class="site-category-dropdown-link-span">
<span class="line-1">SHOP BY</span>
<span class="line-2">Category</span>
</span>
</a>
<ul class="dropdown dropdown-box-shadow">
<% Category.all.each do |root_cat| %>
<li class="has-dropdown site-category-dropdown-element">
<a href="#" class="site-category-dropdown-element-link">
<span class="term"><%= root_cat.name %></span>
</a>
<ul class="dropdown">
<% root_cat.children.each do |children| %>
<li><%= link_to children.name, category_path(id: children.id) %></li>
<% end %>
</ul>
</li>
<% end %>
</ul>
</li>
</ul>
This looks something like the image below ( Root categories and their sub categories is shown on hover)
Now i'm on the store page, and i want to show a drop down similar to the site drop down but only for the products that are being sold by the store.
Store products
Product 1 - (category_id: 46, store_id: 1, product_name: "Prada t-shirt")
Product 2 - (category_id: 47, store_id: 1, product_name: "Prada shoes")
Product 3 - (category_id: 47, store_id: 1, product_name: "Gucci shoes")
Product 4 - (category_id: 12, store_id: 1, product_name: "A classy Dining Table")
Product 5 - (category_id: 12, store_id: 1, product_name: "Kitchen stool")
Product 6 - (category_id: 12, store_id: 1, product_name: "Office Chair")
<br>
cateogory_id 46 is T-shirt in Fashion -> Men -> T-shirt
<br>
category_id 47 is Shoe in Fashion -> Men -> Shoe
<br>
category_id 12 is Furniture in Home -> Furniture
<br>
I'm using the awesome_nested_set gem for the categories (https://github.com/collectiveidea/awesome_nested_set)
i can map all the category_id in an array using:
category_ids = @store.products.map(&:category_id)
My question is, how can i build a drop down similar to the site drop down i showed above but only for products sold by this store. Remember the category_id for each products are the category ids for the leaf category, how do i recreate a drop down from the root categories? Using the store products I've given above, it should look something like this: