4

I'm using the acts_as_commentable_with_threading gem. This gem includes the method 'comment.children', which creates a hash of all the child comments. By default, it orders the children by created_at ASC. I want to change the way the children are ordered, but as far as I know, I cannot directly edit this method. Instead, I've been trying to reorder them in the view like so:

<% @comments = comment.children.order('created_at DESC') %>

Unfortunately, this has no effect. Any idea what I'm doing wrong?

nullnullnull
  • 8,039
  • 12
  • 55
  • 107
  • The last commit to this gems repository was 2 years ago, so it is looks like it is not maintained anymore. – denis.peplin Aug 09 '12 at 03:50
  • Perhaps I'm reading this wrong, but it looks like the last commit was just [three months ago](https://github.com/elight/acts_as_commentable_with_threading/commits/master). – nullnullnull Aug 09 '12 at 20:57
  • Yes, this version is fresh enough. Google often looks for old things, for me it found this: https://github.com/ChristianPeters/acts_as_commentable_with_threading. Same gem name. – denis.peplin Aug 10 '12 at 04:52

1 Answers1

7

You should use sort for arrays and hashes.

<% @comments = comment.children.sort { |a,b| b.created_at <=> a.created_at } %>

Reference: http://apidock.com/ruby/Enumerable/sort

kim3er
  • 6,306
  • 4
  • 41
  • 69
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
  • Your solution definitely works, but I've read from several sources that it's preferable to use .order whenever possible, as .order is handled by the database, which is faster. I'm assuming the .order approach doesn't work here because the array has already been created by the database, so all I can do is manipulate the array with .sort? – nullnullnull Aug 09 '12 at 00:50
  • I'm not that wizard with Ruby, so someone else might as well prove me wrong here, but it seems there isn't an `order` method for arrays. http://www.ruby-doc.org/core-1.9.3/Array.html – Marcelo De Polli Aug 09 '12 at 00:53
  • I'll hold out a bit longer, just in case somebody knows a way to use .order. Otherwise, this makes a lot of sense and I'll accept your answer tomorrow morning. – nullnullnull Aug 09 '12 at 00:55
  • 2
    .order would work if you are composing an active record call. I don't know how acts_as_commentable_with_threading works, but its probably returning children within the database query made to get the comment. Can you show us the class of your children collection? You can do that by adding <%= comment.children.class %> into your view. – caffo Aug 09 '12 at 01:13
  • Yep, the class is Array. Perhaps the best thing to do here is manually insert the gem's controllers into my app and then edit them? – nullnullnull Aug 09 '12 at 20:59
  • 1
    If you are not planning to have a really huge number of children elements, I would stick with .sort instead of trying to modify the gem methods - it can get ugly really fast. Otherwise you may want to check for other gems to help you out. – caffo Aug 09 '12 at 21:05