I've got a comment tree nested in the document, using mongoid embeds_many_recursively like this:
   Document: {
    ...
    comments: [{
      ...
      updated_at,
      child_comments: [{
        ...
        updated_at
        child_comments: [{...},{...}],
        ...},{...}]
      ...}]
    ...}]
   ...}
What's the most effective way of passing it to a view in a way that is ordered by first level 'comment updated_at' attribute?
At the moment I came up with this inside the main document model:
  def flatten_comments
    @flat_comments = []
    self.comments.order_by([[:updated_at, :desc]]).each do |comment|
      flatten_comments_iterator(comment)
    end
    return @flat_comments
  end
  def flatten_comments_iterator(comment)
    @flat_comments << comment
    comment.child_comments.each {|reply| flatten_comments_iterator(reply)}
  end
and then just iterating in the view over the array.
The problems are: 1) in the recursive flattening, the order is lost somewhere, and I can't figure where, step-by-step on paper it seems to adding items in the needed order, probably some thing to do with class variable scope and access.
2) I'm not sure it's the most efficient way to do a simple retrieval.
Would be thankful for an advise and for experience with how to handle this kind of tasks efficiently.
 
     
    