Closed TheKidCoder closed 10 years ago
Hmm... That is could be difficult. Conceptually this could be read as find the first 10 root nodes ordered by state and then get the forests for them. I don't think it is possible to do in a single query. I think it would need to first do the query to get the correct roots, then find forest on all of them, then sort the returned records in Ruby to the same order as the first query returned. The following code (untested) should do what you want:
roots = ApplicationDocument.root.order(state: :asc).limit(10)
trees = ApplicationDocument.where(id: roots.map(&:id)).find_forest
trees.sort_by! { |t| roots.index(t) }
I'm not sure what I think of edge supporting this natively. It would be nice, but it would mean find_forest would need to inspect the already build ARel tree to see if it needs to do the 2 queries.
Thanks.
I figured it wasn't a simple problem. Feel free to close if you would like.
I have a query that I would like to see working:
When I run this I get an error: