diff options
author | Bogdan Gusiev <agresso@gmail.com> | 2012-07-31 10:24:18 +0300 |
---|---|---|
committer | Bogdan Gusiev <agresso@gmail.com> | 2012-07-31 10:24:18 +0300 |
commit | 92641efeecae0910c6292c0c6bf071197b24e63d (patch) | |
tree | da8e0b8ad3660b3b0670d05959f233a414a0836a /guides/source/active_record_querying.textile | |
parent | 10cdeb867bb4b5534c575b0eb3f03e6719269875 (diff) | |
download | rails-92641efeecae0910c6292c0c6bf071197b24e63d.tar.gz rails-92641efeecae0910c6292c0c6bf071197b24e63d.tar.bz2 rails-92641efeecae0910c6292c0c6bf071197b24e63d.zip |
AR::Relation#order: make new order prepend old one
User.order("name asc").order("created_at desc")
# SELECT * FROM users ORDER BY created_at desc, name asc
This also affects order defined in `default_scope` or any kind of associations.
Diffstat (limited to 'guides/source/active_record_querying.textile')
-rw-r--r-- | guides/source/active_record_querying.textile | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index b13932e8cb..dff829a4c1 100644 --- a/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -492,7 +492,7 @@ This code will generate SQL like this: SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5)) </sql> -h3. Ordering +h3(#ordering). Ordering To retrieve records from the database in a specific order, you can use the +order+ method. @@ -518,6 +518,13 @@ Client.order("orders_count ASC, created_at DESC") Client.order("orders_count ASC", "created_at DESC") </ruby> +If you want to call +order+ multiple times e.g. in different context, new order will prepend previous one + +<ruby> +Client.order("orders_count ASC").order("created_at DESC") +# SELECT * FROM clients ORDER BY created_at DESC, orders_count ASC +</ruby> + h3. Selecting Specific Fields By default, <tt>Model.find</tt> selects all the fields from the result set using +select *+. |