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.
There are basically 2 design approaches (one of them being the same as yours), that are documented on ruby driver’s modeling examples. There is also a similar question on SO about it.
About the other issue: there’s nothing bad about recursivity, in general, if the comments don’t have a huge depth of nesting. However your implementation is not thread-safe, as it uses instance variables, and not local variables. To deal with it, you should transform
@flat_commentsto a local variable and pass it as a param toflatten_comments_iteratormethod.Tip: since any method recursion can be transformed to a iteration, what you may want to implement is an iterative preorder traversal of a graph.