I’m looking for a solution for managing categories My categories table has name , parent_id fields My first challenge is to display all categories on a single page and my next is to build select lists represenative of the tree (after this has been done I wanna integrate listings with them)

I don't understand what is challenging about your first challenge. Do you mean you don't know how to get the categories from the database (Category.all) or how to format them on a page or what?

It suggests to me that you have still not followed my and Tamara's advice to work right through Until you understand the basics you are just wasting your time and ours.


how to format them on a page … my thought was to use group_by for formatting a page that displays all categories

how to format them on a page ... my thought was to use group_by for formatting a page that displays all categories

Do the tutorial.


where in the tutorial is the discussion of this

You have not told us what your fundamental problem is, so it is difficult to say. You have just said you want to display all the categories so something like

Category.all.each do |category|   # show data for category end

However since I am telepathic I am guessing that what you actually want to do is show the categories for each parent, which sounds pretty much like user/micropost relationship in the tutorial. The clue here is in the words "each parent". Don't start with the categories and try to group them with the parents, start with the parents and work down, so maybe

Parent.each do |parent|   # show parent data   parent.categories.each do |category|     # show category data   end end


Where did the Parent object come from?

Where did the Parent object come from?

You mentioned that Category has a parent_id field, I deduced from this that you have Parent has_many categories and Category belongs_to parent. If you don't understand what I am talking about then do the tutorial.

If you don't have Parent objects then please explain what you are trying to do.

Colin P.S.

    Q: Why should I start my reply below the quoted text?

    Q: Why is top-posting such a bad thing?

    Q: What makes top-posted replies harder to read than bottom-posted?

    Q: Should I trim down the quoted part of an email to which I'm replying?

right, no Parent model … same thing

right, no Parent model ... same thing

So what exactly are you trying to do?

You said you wanted to show all categories on a page so what is wrong with Category.all.each do |category|   # show category.whatever data end

Did you seem my PostScript on the previous post?


Several years ago, I had a similar desire to have a screen representation of a category tree. I was very surprised that I couldn’t find any pre-baked solution, so I built my own (there may be one out there now of which I am unaware). Unfortunately, I have never got round to making my code public, but I can share some of the design considerations I had to consider.

  1. What sort of tree would I like - I went for creating something along the lines of the windows file explorer - a pretty standard representation.

  2. What elements would this require: a. Database - a category structure - ie. Awesome nested set worked well for me. b. Visual - simple graphics, small images for: node open, node closed, leaf c. Next step was to build a simple mock up on the screen to create the CSS for the tree structure. HTML struture would be nested ul lists for branches and li elements for nodes and leafs. classes for the open and closed state. (I could share this if you like)

  3. Perhaps the most difficult question is the decision of how much I am going to do in JS and how much server side. I considered supplying the whole tree in json format and then using js to build the visual and keep things in step. But it just seemed to get too involved. And took me more into js development than I was comfortable with.

  4. The solution I used was to maintain the tree on the server, and open close, create and delete nodes using ajax calls. Rewriting sections of the tree by replacing nodes by rendering appropriate partials.

  5. Giving each node an id of the category model object id allows the server to manage the tree by js responses that show/hide branches.

  6. It can seem a bit of a daunting task, but if you break it down into the steps above, create open/closed partials that call each other, then build the ajax calls for open/close, it is actually not that hard, and I have a working solution that is common across three projects. Rails 2 and Rails 4.

Hopefully this at least gives some food for thought. Tony