Difficult categories drop out, depending on the leaf nodes present

I have this relationship

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

      

On the home page (view file), I have a category that looks like amazon:

 <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>

      

It looks something like below (root categories and their subcategories are shown on hover) Site category dropdown

Now I am on the store page and I want to show a drop similar to the site disclosure, but only for products that are sold by the store.
Store food

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 am using the awesome_nested_set gem for categories ( https://github.com/collectiveidea/awesome_nested_set )
I can display all category_ids in an array using: category_ids = @ store.products.map (&: category_id)

My question is how can I create a dropdown similar to the dropdown site I showed above, but only for products sold by this store. Remember that the category_id for each product is the category ID for the leaf category, how do I recreate the drop from the root categories? Using the store products I listed above, it should look something like this: Store category drop down, only products sold by the store.

+3


source to share


1 answer


It might be a naive implementation, but I think it might 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 will iterate over the collection just like you did in your original post. This will incur the overhead that @engineersmnky warned about as you will be doing quite a lot of database queries. It might be a good idea to cache all categories in an instance variable before calling 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 misunderstood your question!

+2


source







All Articles