aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/active_record_querying.md
diff options
context:
space:
mode:
authorschneems <richard.schneeman@gmail.com>2014-06-28 14:30:52 -0500
committerschneems <richard.schneeman@gmail.com>2014-06-28 17:49:40 -0500
commitd4fd0bd17709735ac91e434c94fe99429f078c6e (patch)
treed140dfad29eb1b232095fe0d8b96088e3f873285 /guides/source/active_record_querying.md
parentd319ef851a5d5967271ed7169d6c5c1e028951c6 (diff)
downloadrails-d4fd0bd17709735ac91e434c94fe99429f078c6e.tar.gz
rails-d4fd0bd17709735ac91e434c94fe99429f078c6e.tar.bz2
rails-d4fd0bd17709735ac91e434c94fe99429f078c6e.zip
[ci skip] Consolidate docs for `last`
Add docs on what happens when a numerical argument is provided to last. Since `last!` behaves exactly the same way but can raise an argument we can consolidate it in the `last` section.
Diffstat (limited to 'guides/source/active_record_querying.md')
-rw-r--r--guides/source/active_record_querying.md37
1 files changed, 21 insertions, 16 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md
index 2533edc706..a699a3dffe 100644
--- a/guides/source/active_record_querying.md
+++ b/guides/source/active_record_querying.md
@@ -186,7 +186,7 @@ The `first!` method behaves exactly like `first`, except that it will raise `Act
#### `last`
-`Model.last` finds the last record ordered by the primary key. For example:
+The `last` method finds the last record ordered by the primary key. For example:
```ruby
client = Client.last
@@ -199,7 +199,26 @@ The SQL equivalent of the above is:
SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
```
-`Model.last` returns `nil` if no matching record is found and no exception will be raised.
+The `last` method returns `nil` if no matching record is found and no exception will be raised.
+
+You can pass in a numerical argument to the `last` method to return up to that number of results. For example
+
+```ruby
+client = Client.last(3)
+# => [
+ #<Client id: 219, first_name: "James">,
+ #<Client id: 220, first_name: "Sara">,
+ #<Client id: 221, first_name: "Russel">
+]
+```
+
+The SQL equivalent of the above is:
+
+```sql
+SELECT * FROM clients ORDER BY clients.id DESC LIMIT 3
+```
+
+The `last!` method behaves exactly like `last`, except that it will raise `ActiveRecord::RecordNotFound` if no matching record is found.
#### `find_by`
@@ -269,21 +288,7 @@ SELECT * FROM clients WHERE (clients.id IN (1,10))
WARNING: `Model.find(array_of_primary_key)` will raise an `ActiveRecord::RecordNotFound` exception unless a matching record is found for **all** of the supplied primary keys.
-#### last
-
-`Model.last(limit)` 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">]
-```
-
-The SQL equivalent of the above is:
-```sql
-SELECT * FROM clients ORDER BY id DESC LIMIT 2
-```
### Retrieving Multiple Objects in Batches