Deeply nested model

75 views
Skip to first unread message

p.tes...@gmail.com

unread,
Jun 17, 2012, 9:58:12 AM6/17/12
to simple-n...@googlegroups.com
Hi there,

I have a category model build using the ancestry gem.
  • The categories are deeply nested and in theory the tree may be deep as much as the user want
class Category < ActiveRecord::Base
  has_ancestry
  has_many :products
end

Is there a way to define a menu for a given category in such a way that the corresponding menu and breadcrumb contain the full trail of categories to the root?
I think a recursive approach should do the trick but I am struggling to figure out how to implement it.

Thanks for your help.

Paul

extreme....@gmail.com

unread,
Jul 19, 2013, 3:43:42 AM7/19/13
to simple-n...@googlegroups.com, p.tes...@gmail.com
I am attempting to do the same thing. I too came to the conclusion that recursion would do the trick and have since come up with a method I believe should do the job but I have hit a brick wall. On the good side, I get no errors. On the bad side, I get no menu.

If you have managed to come up with a solution, I would be keen to see it.

Regards,
Braden

extreme....@gmail.com

unread,
Jul 19, 2013, 4:26:44 AM7/19/13
to simple-n...@googlegroups.com, p.tes...@gmail.com
I actually got it working thanks to the help from a friend.. here is the code I used (ps. those CSS classes you see are for Foundation):



SimpleNavigation::Configuration.run do |navigation|
  navigation.items do |primary|

    def walktree(db, parent)
      sub_items = pages_menu.where(parent_id: db.id)
      if sub_items.count > 0
        parent.item db.name,db.name,my_content_path(db.id), class: "has-dropdown" do |s|
          s.dom_class="dropdown"
          sub_items.each do |ddItem|
            walktree(ddItem, s)
          end
        end
      else
        parent.item db.name,db.name,my_content_path(db.id)
      end
    end

    primary.dom_class='right'

    #dynamic pages menus
    pages_menu.where(parent_id: '0').each do |menu|
      if menu.is_visible
        walktree(menu, primary)
      end
    end


  end
end

Reply all
Reply to author
Forward
0 new messages