aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorschneems <richard.schneeman@gmail.com>2014-06-28 14:16:49 -0500
committerschneems <richard.schneeman@gmail.com>2014-06-28 17:49:40 -0500
commitd319ef851a5d5967271ed7169d6c5c1e028951c6 (patch)
tree78a1be0bbf5a442b06e5c9126968f72c60b809b1
parent7d9c3ff55c288d277b20c69df4a61c505a99f90c (diff)
downloadrails-d319ef851a5d5967271ed7169d6c5c1e028951c6.tar.gz
rails-d319ef851a5d5967271ed7169d6c5c1e028951c6.tar.bz2
rails-d319ef851a5d5967271ed7169d6c5c1e028951c6.zip
[ci skip] Consolidate docs for `take`
Add docs on what happens when a numerical argument is provided to take. Since `take!` behaves exactly the same way but can raise an argument we can consolidate it in the `take` section.
-rw-r--r--guides/source/active_record_querying.md55
1 files changed, 20 insertions, 35 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md
index ecfc743642..2533edc706 100644
--- a/guides/source/active_record_querying.md
+++ b/guides/source/active_record_querying.md
@@ -113,7 +113,7 @@ SELECT * FROM clients WHERE (clients.id = 10) LIMIT 1
#### `take`
-`Model.take` retrieves a record without any implicit ordering. For example:
+The `take` method retrieves a record without any implicit ordering. For example:
```ruby
client = Client.take
@@ -126,7 +126,25 @@ The SQL equivalent of the above is:
SELECT * FROM clients LIMIT 1
```
-`Model.take` returns `nil` if no record is found and no exception will be raised.
+The `take` method returns `nil` if no record is found and no exception will be raised.
+
+You can pass in a numerical argument to the `take` method to return up to that number of results. For example
+
+```ruby
+client = Client.take(2)
+# => [
+ #<Client id: 1, first_name: "Lifo">,
+ #<Client id: 220, first_name: "Sara">
+]
+```
+
+The SQL equivalent of the above is:
+
+```sql
+SELECT * FROM clients LIMIT 2
+```
+
+The `take!` method behaves exactly like `take`, except that it will raise `ActiveRecord::RecordNotFound` if no matching record is found.
TIP: The retrieved record may vary depending on the database engine.
@@ -214,23 +232,6 @@ This is equivalent to writing:
Client.where(first_name: 'does not exist').take!
```
-#### `take!`
-
-`Model.take!` retrieves a record without any implicit ordering. For example:
-
-```ruby
-client = Client.take!
-# => #<Client id: 1, first_name: "Lifo">
-```
-
-The SQL equivalent of the above is:
-
-```sql
-SELECT * FROM clients LIMIT 1
-```
-
-`Model.take!` raises `ActiveRecord::RecordNotFound` if no matching record is found.
-
#### `last!`
`Model.last!` finds the last record ordered by the primary key. For example:
@@ -268,22 +269,6 @@ 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.
-#### take
-
-`Model.take(limit)` 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">]
-```
-
-The SQL equivalent of the above is:
-
-```sql
-SELECT * FROM clients LIMIT 2
-```
-
#### last
`Model.last(limit)` finds the number of records specified by `limit` ordered by primary key in descending order: