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)
我正在为类别使用awesome_nested_set gem (https://github.com/collectiveidea/awesome_nested_set),我可以使用:category_ids = @store.products.map(&:category_id)映射数组中的所有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:
我的问题是,我如何建立一个像我上面展示的网站下拉一样的下拉,但只针对这个商店出售的产品。记住,每个产品的category_id都是叶子类别的类别id,我如何从根类别中重新创建一个下拉列表?使用我上面给出的商店产品,应该是这样的:
1 个解决方案
#1
2
This might be a naive implementation, but I think it could do the trick.
这可能是一个幼稚的实现,但我认为它可以实现这个目的。
# in your controller
@categories = find_root_categories @store.products.map(&:category_id)
def find_root_categories(leaf_categories)
leaf_categories.map { |node| find_root(node) }.uniq!
end
def find_root(leaf)
return leaf unless leaf.parent_id?
find_root(Category.find(leaf.parent_id))
end
And then you'd iterate over the collection just as you did in your original post. This does incur the overhead that @engineersmnky warned about though, as you'll be making quite a lot of database calls. It might be a good idea to cache all categories in an instance variable before calling find_root
:
然后你会迭代这个集合,就像你在最初的帖子里做的那样。这确实会导致@engineersmnky警告的开销,因为您将进行大量的数据库调用。在调用find_root之前,最好在实例变量中缓存所有类别:
# in the controller
@categories = Category.all
def find_root(leaf)
return leaf unless leaf.parent_id?
find_root(@categories.find(leaf.parent_id))
end
Please let me know if I've misunderstood something about your question!
如果我误解了你的问题,请告诉我。
#1
2
This might be a naive implementation, but I think it could do the trick.
这可能是一个幼稚的实现,但我认为它可以实现这个目的。
# in your controller
@categories = find_root_categories @store.products.map(&:category_id)
def find_root_categories(leaf_categories)
leaf_categories.map { |node| find_root(node) }.uniq!
end
def find_root(leaf)
return leaf unless leaf.parent_id?
find_root(Category.find(leaf.parent_id))
end
And then you'd iterate over the collection just as you did in your original post. This does incur the overhead that @engineersmnky warned about though, as you'll be making quite a lot of database calls. It might be a good idea to cache all categories in an instance variable before calling find_root
:
然后你会迭代这个集合,就像你在最初的帖子里做的那样。这确实会导致@engineersmnky警告的开销,因为您将进行大量的数据库调用。在调用find_root之前,最好在实例变量中缓存所有类别:
# in the controller
@categories = Category.all
def find_root(leaf)
return leaf unless leaf.parent_id?
find_root(@categories.find(leaf.parent_id))
end
Please let me know if I've misunderstood something about your question!
如果我误解了你的问题,请告诉我。