Skip to content

Instantly share code, notes, and snippets.

@spohlenz
Created August 21, 2008 03:34
Show Gist options
  • Save spohlenz/6499 to your computer and use it in GitHub Desktop.
Save spohlenz/6499 to your computer and use it in GitHub Desktop.
class Page < ActiveRecord::Base
acts_as_tree
def self.tree
hash = find(:all).inject({}) { |hash, p| hash[p.id] = [p, []]; hash }
hash.inject([]) { |result, (key, value)|
if parent_id = value.first.parent_id
hash[parent_id].last << value
else
result << value
end
result
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment