active_admin-sortable_tree
active_admin-sortable_tree copied to clipboard
Fix collection sort when not be an integer
Problem
When we use ancestry gem, this gem use ancestry field as string, so #sort_by method trouble problems when compare strings with ids
Solution
I'm using this sortable configuration
sortable \
tree: true,
sorting_attribute: :ancestry,
parent_method: :parent,
children_method: :children,
roots_method: :roots,
roots_collection: proc { collection.where(ancestry: nil) }
And I change this line. Instead of
def build(page_presenter, collection)
# ...
@collection = @collection.sort_by do |a|
a.send(options[:sorting_attribute]) || 1
end
# ...
end
I fix this problem with #to_i
def build(page_presenter, collection)
# ...
@collection = @collection.sort_by do |a|
a.send(options[:sorting_attribute].to_i) || 1
end
# ...
end
Could your model expose an integer version of the attribute; then use that as the sorting attribute?
:smile: Yes, I think we can do that!
Sincerely, it hadn't occurred to me to do it that way
Thanks