order
![Very extensive documentation Importance_5](https://d2vfyqvduarcvs.cloudfront.net/images/importance_5.png?1349367920)
- 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)
- What's this?
order(*args)
public
Allows to specify an order attribute:
User.order(:name) => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC User.order(email: :desc) => SELECT "users".* FROM "users" ORDER BY "users"."email" DESC User.order(:name, email: :desc) => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC, "users"."email" DESC 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
![Default_avatar_30](https://www.gravatar.com/avatar/16b8d429e0123bc50ba8ad27cdaa6ff4?size=30&default=http://apidock.com/images/default_avatar_30.png)
![Default_avatar_30](https://www.gravatar.com/avatar/fdc75e4cf5aa4bf67af27f5ebe19d261?size=30&default=http://apidock.com/images/default_avatar_30.png)
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")
![Default_avatar_30](https://www.gravatar.com/avatar/794349372513ad17e1e61aa7fbff7f1c?size=30&default=http://apidock.com/images/default_avatar_30.png)
![Default_avatar_30](https://www.gravatar.com/avatar/fea701703763aa3128a79d25366f8bfa?size=30&default=http://apidock.com/images/default_avatar_30.png)
Order with hash parameters only in ActiveRecord >= 4.0
If you use order with hash parameters on AR3 versions it wont work.
![Default_avatar_30](https://www.gravatar.com/avatar/996b64c2708771eb4f0c479e0d3a0646?size=30&default=http://apidock.com/images/default_avatar_30.png)
![Default_avatar_30](https://www.gravatar.com/avatar/4e6aa196471a575d9b92752773e51797?size=30&default=http://apidock.com/images/default_avatar_30.png)
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’]
![Default_avatar_30](https://www.gravatar.com/avatar/c2713a4959692f16d27d2553fb06cc4b?size=30&default=http://apidock.com/images/default_avatar_30.png)
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