Suppose I have a collection of Pages that are ordered by a column called :sibling_order. Is there a more efficient way to update the ordering of the collection than what I'm doing below:
class Page < ActiveRecord::Base
...
def update_order(order)
if (order < self.sibling_order)
siblings = Page.where("parent_id = ? AND sibling_order < ? AND sibling_order >= ?",new_parent_id,self.sibling_order,order)
siblings.collect{|s|s.update_attribute(:sibling_order,s.sibling_order + 1)}
elsif (order > self.sibling_order)
siblings = Page.where("parent_id = ? AND sibling_order > ? AND sibling_order <= ?",new_parent_id,self.sibling_order,order)
siblings.collect{|s|s.update_attribute(:sibling_order,s.sibling_order - 1)}
end
self.update_attribute(:sibling_order, order) if self.sibling_order != order
end
...
end