ruby-on-railsruby-on-rails-4activerecordancestry

Rails 4 Active Record ordering lost on subsequent method calls


When I retrieve an ordered active records relation, subsequent method calls on that relation do not maintain the order. For example:

nodes = post.subtree(:to_depth => 1).order(score: :desc).limit(6)
child_ids = nodes.ids

I would expect child_ids to be an array of ids ordered in the same manner that nodes is ordered in (score/ascending). This is not the case. Likewise, when I call

nodes.arrange 

using the ancestry gem, what I get back is not ordered. Is that how it's supposed to behave?


Solution

  • Try this.

    nodes = post.subtree(:to_depth => 1).order('score DESC').limit(6)