Skip to content

Subtrees of multiple nodes #75

Open

Description

@mgornick

I was wondering if there are any plans to support a class method to return the subtrees of multiple nodes.

In this example of 3 trees:

A => B
C => D
E => F

>> Node.subtrees_for([A, B])
=> [A, B, C, D]

Activity

  1. markmoser commented on Aug 20, 2013

    @markmoser

    like this?

    def self.subtree_ids_for roots
      roots.map{ | r | r.subtree_ids}.flatten.uniq
    end
    def self.subtrees_for roots
      self.where(id: self.subtree_ids_for(roots))
    end
    

    is there a better way to do this?

  2. kbrock commented on May 2, 2018

    @kbrock
    Collaborator

    Believe it or not, still working on this one.
    Hoping I'm close.

  3. nflorentin commented on May 17, 2019

    @nflorentin

    Rails 5 added support for "or" queries, so it could be something like that using only 2 queries:

    nodes = Node.all # first query here
    query = nodes[0].subtree
    nodes[1..-1].each do |n|
      query = query.or(n.subtree)
    end
    query.load # second query here
    

    But I think there is a more elegant way to write it...

  4. kbrock commented on May 21, 2019

    @kbrock
    Collaborator

    @nflorentin yes, using ActiveRecord or would get rid of a bunch of arel helpers.

    I'm thinking the next major version of ancestry will probably drop older versions of active record that do not support or.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions