Sort Categories by name

I am by no means a ruby expert, but I think this is a sensible way:

parent_category = Category.where(:name=>"Whatever").first

children = Category.where(:parent_category_id=>parent_category.id)

x = 0
children.order(:name).each do |c|
   puts c.name
   x += 1
   c.position = x
   c.save!()
end;

that’ll output the name of each of the children of parent_category, and assign their position alphabetically.

I tried it on the console and it worked - but I provide no guarentee it won’t cause your server to set on fire or similar :wink: