get all products of category and child categories (rails, awesome_nested_set)

前端 未结 3 1525
孤街浪徒
孤街浪徒 2021-02-15 14:50

having an e-commerce application under development i am trying to get my head around the following problem: I have my categories realized through the awesome_nested_set plugin.

3条回答
  •  梦谈多话
    2021-02-15 15:36

    There are a several ways you could improve your code, but if you're looking for all products that belong to a category and children (descendants) of the category then why you won't do it like this:

    def branch_ids
      self_and_descendants.map(&:id).uniq 
    end
    
    def all_products
      Product.find(:all, :conditions => { :category_id => branch_ids } )
    end
    

    I've done some assumptions here, but I hope you get the idea.

提交回复
热议问题