Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select all children for a list of parents. #280

Closed
teoljungberg opened this issue May 23, 2016 · 7 comments
Closed

Select all children for a list of parents. #280

teoljungberg opened this issue May 23, 2016 · 7 comments

Comments

@teoljungberg
Copy link

I have a use-case where I want to receive all children for a list of parents. But, with searching the issues and the code base itself, I couldn't figure out how to do so.

I opted for a simple solution for now, but I feel like there must be a achieve the same result from using ancestry. Could we construct the same query we do for children but called on a relation instead?

@kbrock
Copy link
Collaborator

kbrock commented May 23, 2016

This is tricky. Off hand, I think I would use an OR. Sorry that this example uses inject:

def all_children_of(parents)
  # don't modify parameters passed in
  parents = parents.dup
  # start query with children of first parent
  scope = Model.children_of(parents.pop)
  # build up the scope to include the children of other parents.
  parents.inject(scope) { |scope, parent| scope.or(Model.children_of(parent) }
end

You also could use subtree_of instead of children_of

@teoljungberg
Copy link
Author

@kbrock Thanks for the quick answer. Maybe I'm missing something here, but #or is not defined on AR::Relation in Rails 4.2 - that's introduced in Rails 5.

@stefankroes
Copy link
Owner

If you really only want the children of a set of parents (not subtrees or descendants), you can use an 'in' query as all children of a certain parent have the same ancestry value. I believe a parent should have a method child_ancestry or something along those lines. You could map the parents their child ancestries and select nodes where ancestry is included in this array (Rails will generate the correct query automatically).

@kbrock
Copy link
Collaborator

kbrock commented Oct 7, 2016

@teoljungberg I just ran into this problem as well. I don't have the nodes in memory either. Has me wanting to move some stuff around in a drastic way. I'll keep you posted

@kbrock
Copy link
Collaborator

kbrock commented Nov 1, 2016

Found a better rails 5 version

def all_children_of(parents)
  parents.inject(Model.none) { |scope, parent| scope.or(Model.children_of(parent) }
end

Rails 4 probably would need to use the conditions. It looks up all parents and it is kinda an internal api, but sometimes you want it to work for now:

def all_children_of(parents)
  parents.inject(nil) { |scope, parent| s = parent.children_condition ; scope.nil? ? s : scope.or(s) } end

Do note, rails 5 uses ActiveRecord or while rails 4 uses Arel or

@kbrock
Copy link
Collaborator

kbrock commented Dec 14, 2016

I think this is similar to #308

@kbrock
Copy link
Collaborator

kbrock commented Aug 24, 2018

marking as a duplicate

@kbrock kbrock closed this as completed Aug 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants