Reorder rails result set

It's amazing to think that I haven't need this before now, but today I needed to reorder a Rails result set that already had an order on it. When you already have an ActiveRecord::Relation that has an order on it, when you apply another order method, it will just add those to to the end of the order that is already there.

What happens when you want to totally reorder? Do you have to remove the first order or make it conditional? Nope.

The amazing Rails core team thought that through and they provide the .reorder() method that will override the original order in the ActiveRecord::Relation and replaces it with the new order.

Love Rails. Great framework to work with!

Share this post