My goal is to use nested <ul> elements to display a hierarchical view of an entire table. It is my understanding that all acts_as_tree does is make available 3 additional methods: ''ancestors'', ''root'', and ''siblings''. As useful as these methods may be, I don''t see how they are going to help me achieve my goal. It seems to me that I''m additionally going to need to somehow get the table sorted properly; and, even after I have done that I''m still left with the difficult issue of keeping track of levels and opening and closing <ul> tags appropriately. Am I missing something? Thanks for any input. ... doug
Doug Jolley wrote:> My goal is to use nested <ul> elements to display a hierarchical view > of an entire table. It is my understanding that all acts_as_tree does > is make available 3 additional methods: ''ancestors'', ''root'', and > ''siblings''. As useful as these methods may be, I don''t see how they > are going to help me achieve my goal. It seems to me that I''m > additionally going to need to somehow get the table sorted properly; > and, even after I have done that I''m still left with the difficult > issue of keeping track of levels and opening and closing <ul> tags > appropriately. Am I missing something? > > Thanks for any input. > > ... dougDoug, use siblings() to iterate through the node''s children and put a <li> for each, if the child itself has children then wash and repeat.. think helper functions and recursion.. ilan -- Posted via http://www.ruby-forum.com/.
Ilan Berci wrote: [...]> > use siblings() to iterate through the node''s children and put a <li> for > each, if the child itself has children then wash and repeat.. think > helper functions and recursion..That''s pretty inefficient on a tree of any size, at least if you''re doing a DB query each time instead of loading the whole tree into memory. Instead, use awesome_nested_set or similar. It provides a data structure much better suited to what you want.> > ilanBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser wrote:> > That''s pretty inefficient on a tree of any size, at least if you''re > doing a DB query each time instead of loading the whole tree into > memory. >Very true! However, if only a couple of branches will be opened for a given tree and the branches will be opened via an AJAX call then the performance will most likely be better than retrieving the whole tree into memory initially and then trucking it across the wire.. depends upon the situation I guess... Anyways, question smelled like a homework assignment so I didn''t reason on performance.. ilan -- Posted via http://www.ruby-forum.com/.
> use siblings() to iterate through the node''s children and put a <li> for > each, if the child itself has children then wash and repeat.. think > helper functions and recursion..Thanks to both contributors for the input. This is not a homework assignment. It represents an actual need; but, I would like to treat it as a learning experience. The tree involved is going to be pretty small, I''m guessing typically 10 - 20 nodes. I think my best approach would be to begin with acts_as_tree; and, when, as, and if I ever get that working then try advancing to a nested set approach. (Pro Active Record says that nested sets can be "a little complex to grasp and manage". That scares me at my level of understanding.) Having said that, please allow me to clarify the suggested acts_as_tree approach.> use siblings() to iterate through the node''s childrenBy "the nodes children" I think that you mean the children of each of the root nodes. Am I correct?> put a <li> for each child, if the child itself has children then wash and repeatTranslation: For each child of a given root node: if that child has no children put an <li> for that child. Else put a <ul> and begin processing the grandchildren I think that I get the concept. I''m going to give it a whirl. I will likely have additional questions. However, it could be days before I discover them. :) ... doug
Doug Jolley wrote:> > This is not a homework assignment. It represents an actual need; but, > I would like to treat it as a learning experience. The tree involved > is going to be pretty small, I''m guessing typically 10 - 20 nodes. I > think my best approach would be to begin with acts_as_tree; and, when, > as, and if I ever get that working then try advancing to a nested set > approach. (Pro Active Record says that nested sets can be "a little > complex to grasp and manage". That scares me at my level of > understanding.) >Doug, Then I would use what Marmen suggested: awesome_nested_set as while the earlier approach is probably easier to implement, it is not efficient. I mistakenly assumed that it was a homework assignment (as we get a lot of questions on this list for that) and was just giving you the "get it done" approach ilan -- Posted via http://www.ruby-forum.com/.
Doug Jolley wrote:> (Pro Active Record says that nested sets can be "a little > complex to grasp and manage". That scares me at my level of > understanding.)Perhaps they can be, but awesome_nested_set encapsulates the complexity. And they''re not really that complex to begin with. Have you read about the nested-set data structure? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
> Have you read about the nested-set data structure?Yes; but, not in detail. The section on nested sets immediately follows the section on acts_as_tree in "Pro-Active Record". I take it that awesome_nested_set is not the same thing as acts_as_nested_set? In any event, here''s the deal. I just stumbled onto acts_as_tree a few days ago. Before that, I was oblivious to it. Because I was off in oblivion, I hardcoded a solution for creating the hierarchical view. (It took me about 2 weeks! :) ) It uses a linked-records approach where each record has a pointer to its ancestor much like acts_as_tree. Surprisingly, it seems to work just fine; however, the code is about 100 lines in length and its messy. I''m sure that my code is even less efficient than act_as_tree; and, as I say, it seems to work just fine. So, I don''t think that performance is going to be an issue. I''d like to clean up my messy code. I think that if I use acts_as_tree, I can use my existing database. I just have to be sure to declare my foreign key since I named my pointer column ''parent'' rather than the default, ''parent_id''. If I use a nested-set approach, I would have to modify/re-create the database. In addition, based on Ilan''s comments, I have a vague idea of how to proceed with acts_as_tree where I am much more in the dark with nested sets. All in all and despite your undoubtedly good advice to the contrary, I would like to proceed with a acts_as_tree solution. That doesn''t mean that I won''t re-do it with nested sets at some future date. I just want to make sure that I can walk before I try flying. I hope that you guys will stick with me through my adventure which I''m not even going to be able to begin for a couple of days because of other things which have arisen. Thanks ever so much for your awesome help. ... doug
Doug Jolley wrote:>> �Have you read about the nested-set data structure? > > Yes; but, not in detail. The section on nested sets immediately > follows the section on acts_as_tree in "Pro-Active Record".I''ve never heard of Pro-Active Record, and it sounds very much like it is steering you wrong.> > I take it that awesome_nested_set is not the same thing as > acts_as_nested_set?They are different plugins. When I did my research a few months ago, awesome_nested_set seemed the better choice.> > In any event, here''s the deal. I just stumbled onto acts_as_tree a > few days ago. Before that, I was oblivious to it. Because I was off > in oblivion, I hardcoded a solution for creating the hierarchical > view. (It took me about 2 weeks! :) ) It uses a linked-records > approach where each record has a pointer to its ancestor much like > acts_as_tree.Yes, the classic adjacency list structure.> Surprisingly, it seems to work just fine; however, the > code is about 100 lines in length and its messy. I''m sure that my > code is even less efficient than act_as_tree; and, as I say, it seems > to work just fine. So, I don''t think that performance is going to be > an issue. I''d like to clean up my messy code.OK.> I think that if I use > acts_as_tree, I can use my existing database. I just have to be sure > to declare my foreign key since I named my pointer column ''parent'' > rather than the default, ''parent_id''. If I use a nested-set approach, > I would have to modify/re-create the database.You would need to make a very small change (add two numeric fields and run one or two simple statements in the migration). It''s quite easy.> In addition, based on > Ilan''s comments, I have a vague idea of how to proceed with > acts_as_tree where I am much more in the dark with nested sets.Then learn about nested sets. The principle is not difficult, and the data structure works much better. Joe Celko''s articles are a good place to start.> All > in all and despite your undoubtedly good advice to the contrary, I > would like to proceed with a acts_as_tree solution.It''s your decision, but be aware that based on what you''ve posted, you''re basing your decision on the wrong criteria.> That doesn''t > mean that I won''t re-do it with nested sets at some future date. I > just want to make sure that I can walk before I try flying.It doesn''t work like that here. It''s more like trying to walk from New York to Boston because learning to drive a car seems too hard.> I hope > that you guys will stick with me through my adventure which I''m not > even going to be able to begin for a couple of days because of other > things which have arisen. > > Thanks ever so much for your awesome help.You''re most welcome. Again, please choose a data structure that actually suits your needs, not one that looks simpler but won''t do the job.> > ... dougBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Thanks, Marmen. I will give your undoubtedly good advice serious consideration. As I said, I''m going to have to take a break from this project for a couple of days anyway in order to tend to some other things that have arisen. Thanks again. ... doug