Skip to content

Instantly share code, notes, and snippets.

View calvincorreli's full-sized avatar

Calvin Correli calvincorreli

View GitHub Profile
@calvincorreli
calvincorreli / sorting_nested_set_children.rb
Created November 2, 2010 11:55
Reordering children using Sortable with nested_set / Awesome nested_set, and similar
def reorder_children(ordered_ids)
ordered_ids = ordered_ids.map(&:to_i)
current_ids = children.map(&:id)
unless current_ids - ordered_ids == [] && ordered_ids - current_ids == []
raise ArgumentError, "Not ordering the same ids that I have as children. My children: #{current_ids.join(", ")}. Your list: #{ordered_ids.join(", ")}. Difference: #{(current_ids - ordered_ids).join(', ')} / #{(ordered_ids - current_ids).join(', ')}"
end
j = 0
transaction do
for new_id in ordered_ids
old_id = current_ids[j]