method

order

order(*args)
public

No documentation available.

# File activerecord/lib/active_record/relation/query_methods.rb, line 58
    def order(*args)
      return self if args.blank?

      relation = clone
      relation.order_values += args.flatten
      relation
    end

5Notes

Reorder

stevo · Mar 27, 20127 thanks

If you want to override previously set order (even through default_scope), use reorder() instead.

E.g.

User.order('id ASC').reorder('name DESC')

would ignore ordering by id completely

Ordering on associations

rin · Oct 28, 20142 thanks

For ordering on the attribute of an associated model you have to include it:

Package.includes(:package_size).order("package_sizes.sort_order")

using hash as order

vitaly · Jan 12, 20141 thank

order can be specified as a hash, e.g.:

order(id: :desc)

This will prevent "ambiguous column" errors when the order is used with joins or includes.

Order with hash parameters only in ActiveRecord >= 4.0

spacelement · Oct 30, 20141 thank

If you use order with hash parameters on AR3 versions it wont work.

reorder

bzsolt · Feb 12, 2015

adding to stevo's comment that reorder is also usefull when you have default scope in your model. eg: default_scope -> { order(created_at: :desc) }