method
order
v3.1.0 -
Show latest stable
- Class:
ActiveRecord::QueryMethods
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
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
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
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
If you use order with hash parameters on AR3 versions it wont work.
reorder
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) }