diff options
author | Rafael Magana <raf.magana@gmail.com> | 2012-05-28 11:20:13 -0500 |
---|---|---|
committer | Rafael Magana <raf.magana@gmail.com> | 2012-05-28 11:20:13 -0500 |
commit | 724d6fa6bb48b61798c223b47dd27155839c40d5 (patch) | |
tree | 00373753b049ef1e57b2f456a7488695e46cdd2f | |
parent | 501d98b812202d87c11a68d0d78d26f6befd47a7 (diff) | |
download | rails-724d6fa6bb48b61798c223b47dd27155839c40d5.tar.gz rails-724d6fa6bb48b61798c223b47dd27155839c40d5.tar.bz2 rails-724d6fa6bb48b61798c223b47dd27155839c40d5.zip |
[AR querying guide] Add examples for take(limit), first(limit) and last(limit)
-rw-r--r-- | guides/source/active_record_querying.textile | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index 294ef25b33..eceeb814fd 100644 --- a/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -259,6 +259,54 @@ SELECT * FROM clients WHERE (clients.id IN (1,10)) WARNING: <tt>Model.find(array_of_primary_key)</tt> will raise an +ActiveRecord::RecordNotFound+ exception unless a matching record is found for <strong>all</strong> of the supplied primary keys. +h5. take + +<tt>Model.take(limit)</tt> retrieves the first number of records specified by +limit+ without any explicit ordering: + +<ruby> +Client.take(2) +# => [#<Client id: 1, first_name: "Lifo">, + #<Client id: 2, first_name: "Raf">] +</ruby> + +The SQL equivalent of the above is: + +<sql> +SELECT * FROM clients LIMIT 2 +</sql> + +h5. first + +<tt>Model.first(limit)</tt> finds the first number of records specified by +limit+ ordered by primary key: + +<ruby> +Client.first(2) +# => [#<Client id: 1, first_name: "Lifo">, + #<Client id: 2, first_name: "Raf">] +</ruby> + +The SQL equivalent of the above is: + +<sql> +SELECT * FROM clients LIMIT 2 +</sql> + +h5. last + +<tt>Model.last(limit)</tt> finds the number of records specified by +limit+ ordered by primary key in descending order: + +<ruby> +Client.last(2) +# => [#<Client id: 10, first_name: "Ryan">, + #<Client id: 9, first_name: "John">] +</ruby> + +The SQL equivalent of the above is: + +<sql> +SELECT * FROM clients ORDER By id DESC LIMIT 2 +</sql> + h4. Retrieving Multiple Objects in Batches We often need to iterate over a large set of records, as when we send a newsletter to a large set of users, or when we export data. |