Active Record associations for hierarchical structure with closure/bridge table

Thanks! This gem looks like a great learning resource. Unfortunately it can’t be used in its current state as it doesn’t support multiple parents:

Does this gem support multiple parents?

No. This gem’s API is based on the assumption that each node has either 0 or 1 parent.

The underlying closure tree structure will support multiple parents, but there would be many breaking-API changes to support it. I’m open to suggestions and pull requests.

ClosureTree::Model indeed has belongs_to and has_many relationships (withouth a through option), meaning that a single parent foreign key needs to be encoded in the model table.

Other observations:

  • We currently have an absolute ordering across all nodes (using Content.position) but would like (although we’re not sure how feasible this would be) a specific order for the descendants of each ancestor. It looks like the gem does not support that.
  • Based on the code in ClosureTree::HierarchyMaintenance, my understanding is that updates in the hierarchy table are made in an after_save hook after the default Active Record instructions are run, meaning that an extra SQL statement is made every time. For example when adding a child to a node, AR supposedly issues an insert statement to create a row representing the direct parent-child relationship, then the other needed rows (i.e the relationships with grandparents, grandchildren etc) are created in after_save. Conceptually, from a data integrity and from a performance point of view, it makes more sense to me to create all of these at once.