order
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (0)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (20)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (38)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
order(*args)
public
Applies an ORDER BY clause to a query.
#order accepts arguments in one of several formats.
symbols
The symbol represents the name of the column you want to order the results by.
User.order(:name) # SELECT "users".* FROM "users" ORDER BY "users"."name" ASC
By default, the order is ascending. If you want descending order, you can map the column name symbol to :desc.
User.order(email: :desc) # SELECT "users".* FROM "users" ORDER BY "users"."email" DESC
Multiple columns can be passed this way, and they will be applied in the order specified.
User.order(:name, email: :desc) # SELECT "users".* FROM "users" ORDER BY "users"."name" ASC, "users"."email" DESC
strings
Strings are passed directly to the database, allowing you to specify simple SQL expressions.
This could be a source of SQL injection, so only strings composed of plain column names and simple function(column_name) expressions with optional ASC/DESC modifiers are allowed.
User.order('name') # SELECT "users".* FROM "users" ORDER BY name User.order('name DESC') # SELECT "users".* FROM "users" ORDER BY name DESC User.order('name DESC, email') # SELECT "users".* FROM "users" ORDER BY name DESC, email
Arel
If you need to pass in complicated expressions that you have verified are safe for the database, you can use Arel.
User.order(Arel.sql('end_date - start_date')) # SELECT "users".* FROM "users" ORDER BY end_date - start_date
Custom query syntax, like JSON columns for PostgreSQL, is supported in this way.
User.order(Arel.sql("payload->>'kind'")) # SELECT "users".* FROM "users" ORDER BY payload->>'kind'
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")
Order with hash parameters only in ActiveRecord >= 4.0
If you use order with hash parameters on AR3 versions it wont work.
Careful with arel column
I use what @equivalent suggests a lot but be careful, you MUST state the direction otherwise methods like take/last/first will fail silently!
StatusChange.arel_table[‘created_at’]. asc
not:
StatusChange.arel_table[‘created_at’]
arel_table order by
More objected way how to achieve ORDOR BY .… DESC is like this :
class User < ActiveRecord::Base has_many :status_changes def latest_status_change status_changes .order(StatusChange.arel_table['created_at'].desc) .first end end class StatusChange < ActiverRecord::Base belongs_to :user end
resulting in:
SELECT "status_changes".* FROM "status_changes" WHERE "status_changes"."user_id" = 1 ORDER BY "status_changes"."created_at" DESC
Benefits:
-
you are strictly bound to Modelclass name => renaming table in model will not break the sql code (of if it will, it will explicitly break the syntax on Ruby level, not DB level)
-
you still have the benefit of explicitly saying what table.column the order should be
-
easier to re-factor parts to Query Objects