ClosureTree/closure_tree

Warning : Scoped order is ignored, it's forced to be batch order.

vala opened this issue · 0 comments

vala commented

Hi there,

When using closure_tree order: "column", I get an ActiveRecord warning when I create a record that calls the ClosureTree::HierarchyMaintenance#rebuild! method.

The offending line is the following line : https://github.com/ClosureTree/closure_tree/blob/master/lib/closure_tree/hierarchy_maintenance.rb#L85

children.find_each { |c| c.rebuild!(true) }

Actually closure_tree, when called with the :order keyword argument calls a method called has_many_order_with_option (https://github.com/ClosureTree/closure_tree/blob/master/lib/closure_tree/support.rb#L91) which defines a default order scope on the :children relation.

As ActiveRecord does not support ordering scopes with .find_each (see here : https://github.com/rails/rails/blob/main/activerecord/lib/active_record/relation/batches.rb#L214-L216 and here : https://github.com/rails/rails/blob/main/activerecord/lib/active_record/relation/batches.rb#L286-L294), the HierarchyMaintenance#rebuild! method throws a warning.

One workaround is to call .unscope(:order) before calling the .find_each method.

Is this something that you'd want to fix ?

If so I can submit a PR for this.