我有这些关系
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
在主页(查看文件)中,我有一个类似于亚马逊的类别下拉列表:
<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>
这看起来像下图(根类别及其子类别显示在悬停上)
现在我在商店页面上,我想显示一个类似于网站下拉菜单的下拉菜单,但仅适用于商店正在销售的产品。
商店产品
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>
我将 awesome_nested_set gem 用于类别(https://github.com/collectiveidea/awesome_nested_set)
我可以使用以下方法将所有 category_id 映射到数组中: category_ids = @store.products.map(&:category_id)
我的问题是,我如何构建一个类似于我上面显示的站点下拉列表但仅适用于该商店销售的产品的下拉列表。记住每个产品的 category_id 是叶类别的类别 ID,我如何从根类别重新创建下拉列表?使用我上面给出的商店产品,它应该看起来像这样: