diff options
author | schneems <richard.schneeman@gmail.com> | 2014-06-28 14:38:16 -0500 |
---|---|---|
committer | schneems <richard.schneeman@gmail.com> | 2014-06-28 17:49:40 -0500 |
commit | 63f4155596d1403dbf4972adc0d5877c1ea6d2b0 (patch) | |
tree | 622b4f0742207c7b27af11c2b39ed85afc546e7d /guides/source | |
parent | d4fd0bd17709735ac91e434c94fe99429f078c6e (diff) | |
download | rails-63f4155596d1403dbf4972adc0d5877c1ea6d2b0.tar.gz rails-63f4155596d1403dbf4972adc0d5877c1ea6d2b0.tar.bz2 rails-63f4155596d1403dbf4972adc0d5877c1ea6d2b0.zip |
[ci skip] Consolidate docs for `find`
Put all options for overloading `find` in one section
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/active_record_querying.md | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index a699a3dffe..0b6f40c129 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -93,9 +93,9 @@ The primary operation of `Model.find(options)` can be summarized as: Active Record provides several different ways of retrieving a single object. -#### Using a Primary Key +#### `find` -Using `Model.find(primary_key)`, you can retrieve the object corresponding to the specified _primary key_ that matches any supplied options. For example: +Using the `find` method, you can retrieve the object corresponding to the specified _primary key_ that matches any supplied options. For example: ```ruby # Find the client with primary key (id) 10. @@ -109,7 +109,23 @@ The SQL equivalent of the above is: SELECT * FROM clients WHERE (clients.id = 10) LIMIT 1 ``` -`Model.find(primary_key)` will raise an `ActiveRecord::RecordNotFound` exception if no matching record is found. +The `find` method will raise an `ActiveRecord::RecordNotFound` exception if no matching record is found. + +You can also use this method to query for multiple objects. Call the `find` method and pass in an array of primary keys. The return will be an array containing all of the matching records for the supplied _primary keys_. For example: + +```ruby +# Find the clients with primary keys 1 and 10. +client = Client.find([1, 10]) # Or even Client.find(1, 10) +# => [#<Client id: 1, first_name: "Lifo">, #<Client id: 10, first_name: "Ryan">] +``` + +The SQL equivalent of the above is: + +```sql +SELECT * FROM clients WHERE (clients.id IN (1,10)) +``` + +WARNING: The `find` method will raise an `ActiveRecord::RecordNotFound` exception unless a matching record is found for **all** of the supplied primary keys. #### `take` @@ -268,28 +284,6 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 `Model.last!` raises `ActiveRecord::RecordNotFound` if no matching record is found. -### Retrieving Multiple Objects - -#### Using Multiple Primary Keys - -`Model.find(array_of_primary_key)` accepts an array of _primary keys_, returning an array containing all of the matching records for the supplied _primary keys_. For example: - -```ruby -# Find the clients with primary keys 1 and 10. -client = Client.find([1, 10]) # Or even Client.find(1, 10) -# => [#<Client id: 1, first_name: "Lifo">, #<Client id: 10, first_name: "Ryan">] -``` - -The SQL equivalent of the above is: - -```sql -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. - - - ### 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. |