diff options
Diffstat (limited to 'guides/source/active_record_querying.textile')
-rw-r--r-- | guides/source/active_record_querying.textile | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index f71a136b22..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 *+. @@ -1132,21 +1139,6 @@ Using a class method is the preferred way to accept arguments for scopes. These category.posts.created_before(time) </ruby> -h4. Working with scopes - -Where a relational object is required, the +scoped+ method may come in handy. This will return an +ActiveRecord::Relation+ object which can have further scoping applied to it afterwards. A place where this may come in handy is on associations - -<ruby> -client = Client.find_by_first_name("Ryan") -orders = client.orders.scoped -</ruby> - -With this new +orders+ object, we are able to ascertain that this object can have more scopes applied to it. For instance, if we wanted to return orders only in the last 30 days at a later point. - -<ruby> -orders.where("created_at > ?", 30.days.ago) -</ruby> - h4. Applying a default scope If we wish for a scope to be applied across all queries to the model we can use the +default_scope+ method within the model itself. |