I plan on creating a table of topics. The thing is that the topics
are hierarchical in nature. That is, there may be any number of root
topics. Each root topic can have other topics which would be like sub-
topics; and, sub-topics can have their own sub-topics, etc. I was
thinking about establishing the relationship by having a ‘parent’
column (sort of like ‘belongs_to’) in the topics table so that the
parent topic of any given topic would always be known. However, I’m
not married to this idea. In any event, the problem comes in creating
the show view. I’d like to display the topics using a series of
embedded ULs so that the view not only shows the topics but also shows
them in a way that displays their hierarchical relationships. I have
no idea how I am going to do this. Any ideas?
embedded ULs so that the view not only shows the topics but also shows
them in a way that displays their hierarchical relationships. Â I have
no idea how I am going to do this. Â Any ideas?
If I understand correctly you are looking for a tree structure for the
topics. Have a look at the acts_as_tree plugin. Google will provide
lots of links I think. There is a railscasts at
embedded ULs so that the view not only shows the topics but also shows
them in a way that displays their hierarchical relationships. Â I have
no idea how I am going to do this. Â Any ideas?
If I understand correctly you are looking for a tree structure for the
topics. Have a look at the acts_as_tree plugin.
[…]
I would generally recommend a nested-set model instead;
awesome_nested_set works well. The nested set structure is less obvious
than the simple adjacency list that acts_as_tree provides, but it makes
certain common operations much more efficient.
I would generally recommend a nested-set model instead;
Thanks for the suggestion. I see the nested-set model as being an
alternative to acts_as_tree. Unless I am mistaken they are 2
different ways of doing essentially the same thing. Both could be
helpful in creating a hierarchical view but neither provides a slam-
dunk approach to that end.
I think I see where I stand. Thanks to all who responded.
I would generally recommend a nested-set model instead;
Thanks for the suggestion. I see the nested-set model as being an
alternative to acts_as_tree. Unless I am mistaken they are 2
different ways of doing essentially the same thing.
Yes. Nested sets are unquestionably the superior alternative – for
example, with nested sets, it is possible to retrieve a node and all its
descendants from the DB with a single query. This is impossible with
the simple adjacency list that acts_as_tree provides.
Both could be
helpful in creating a hierarchical view but neither provides a slam-
dunk approach to that end.
What do you mean by a slam-dunk appropach? Something that generates the
HTML? So far as I know, you’ll have to do that yourself.
different ways of doing essentially the same thing. Both could be
helpful in creating a hierarchical view but neither provides a slam-
dunk approach to that end.
Rails still requires “programming,” after all. Are you having trouble
creating an unordered list in your view or something?
If I understand correctly you are looking for a tree structure for the
topics. Have a look at the acts_as_tree plugin. Google will provide
lots of links I think. There is a railscasts athttp://railscasts.com/episodes/162-tree-based-navigation
Thanks for the input, Colin.
I watched the railscast that you suggested. It’s definitely dealing
with the type of thing that I am talking about. However (and I may be
totally naive on this) it seems that acts_as_tree really only does 2
things: (1) it does the grunt work of setting up the associations;
and, (2) it provides a few handy instance methods.
The point is that while having those capabilities available in a nice
package is handy, there doesn’t seem to be anything in the package
that will help me develop the code to generate the basic framework for
the hierarchical view. That’s the part that I don’t have a clue as to
how I am going to accomplish.
You need to look at both client side representation and the server
side data model. For server side, people have been pointing out
solutions here, e.g., acts_as_tree, nested sets etc. Then there is
the client side representation of this, which has not been mentioned.
Recently, I did a project (actually, I am still working on it) where
both sides are important. I used acts_as_tree to begin with and have
moved on to acts_as_adjacency_list since it has a few improvements
that I need.
For the client side, you have two options:
You can either represent the menu trail by a series of linked
“breadcrumbs” which show exactly how far deep you are into the
hierarchy and allow you to climb-back or drill-down further in the
tree. This does not require Javascript and can be done by simple and
traditional view coding.
For fancier navigation where you actually show the tree visually,
you will need Javascript. I am using jQuery based jsTree though there
are a number of other alternatives. You will need some Javascript/
jQuery skills though to go down this path. Take a look at the Spree
project that uses both quite effectively. Actually, I am learning a
lot just digging down into the project code base and adapting it to my
own needs - Highly recommended.
Rails still requires “programming,” after all. Are you having trouble
creating an unordered list in your view or something?
It probably is an ordered tree in a view…? One way I can think of
would be to catch all root elements (usually the ones with a nil
parent_id!) together with der descendants into an array of arrays and
iterate over this with @root_elements_and_all_its_descendents.each do |
x|{ x.each do |z|{z.xyz}} - is that the problem?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.