Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Nested models

nested tree model acts_as_tree acts_as_sane_tree

  • Please log in to reply
1 reply to this topic

#1 jack

jack

    Signalman

  • Moderators
  • 19 posts
  • LocationPoole

Posted 11 February 2014 - 04:28 PM

Hey everyone.

 

I have a quick question about tree type structures in Rails. In my case, my model has a "parent_id" column, linking leaves to branches.

 

There's a lovely gem called "acts_as_tree" (https://github.com/rails/acts_as_tree) originally written by DHH which appears to be a little unloved. Someone else has picked up this project and kept it up to date (https://github.com/amerine/acts_as_tree) which is excellent.

 

However, this gem is missing some features such as depth and nodes_within counts. These features were added to a branch of DHH's original gem at: https://github.com/c...ts_as_sane_tree. Unfortunately this acts_as_sane_tree gem hasn't been touched for two years, and blows up with a deprecation error in Rails 4.

 

My questions to you guys are:

 

Is there a better or more up-to-date gem out there?

Is there an awesome/better way to manage these nested/tree structures?

What experiences or tips do you have regarding this kind of operation?

 

Cheers!



#2 jack

jack

    Signalman

  • Moderators
  • 19 posts
  • LocationPoole

Posted 11 February 2014 - 09:22 PM

Okay, so following a little more research into this, I think I have found a far superior solution.

 

My previously described method was going to be very slow because of the number of SQL queries needed. However, there's a gem called "ancestry" (https://github.com/s...nkroes/ancestry) which stores hierarchy information in its own column, in such a way that only one query is needed.

 

There's a fantastic (although a little old) video tutorial for this on RailsCasts at: http://railscasts.co...s-with-ancestry

 

So unless anyone has any other suggestions, I think this will be the approach I take.







Also tagged with one or more of these keywords: nested, tree, model, acts_as_tree, acts_as_sane_tree

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users