-
Notifications
You must be signed in to change notification settings - Fork 463
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
Comments
This is tricky. Off hand, I think I would use an 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 |
@kbrock Thanks for the quick answer. Maybe I'm missing something here, but |
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). |
@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 |
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 |
I think this is similar to #308 |
marking as a duplicate |
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 forchildren
but called on a relation instead?The text was updated successfully, but these errors were encountered: