Organise ActiveRecord model into a tree structure