diff options
author | schneems <richard.schneeman@gmail.com> | 2014-06-28 14:07:25 -0500 |
---|---|---|
committer | schneems <richard.schneeman@gmail.com> | 2014-06-28 17:49:40 -0500 |
commit | 7d9c3ff55c288d277b20c69df4a61c505a99f90c (patch) | |
tree | fcc4dead9e2a3e27407f1c250b751bbeaf8fd540 /guides | |
parent | caa25e9fc3862ea036c81d7b7d58101b6bb79cb8 (diff) | |
download | rails-7d9c3ff55c288d277b20c69df4a61c505a99f90c.tar.gz rails-7d9c3ff55c288d277b20c69df4a61c505a99f90c.tar.bz2 rails-7d9c3ff55c288d277b20c69df4a61c505a99f90c.zip |
[ci skip] Consolidate docs for `find_by`
Since `find_by!` behaves exactly the same way but can raise an argument we can consolidate it in the `find_by` section.
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_querying.md | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 0d3a1dc948..ecfc743642 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -185,7 +185,7 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 #### `find_by` -`Model.find_by` finds the first record matching some conditions. For example: +The `find_by` method finds the first record matching some conditions. For example: ```ruby Client.find_by first_name: 'Lifo' @@ -201,6 +201,19 @@ It is equivalent to writing: Client.where(first_name: 'Lifo').take ``` +The `find_by!` method behaves exactly like `find_by`, except that it will raise `ActiveRecord::RecordNotFound` if no matching record is found. For example: + +```ruby +Client.find_by! first_name: 'does not exist' +# => ActiveRecord::RecordNotFound +``` + +This is equivalent to writing: + +```ruby +Client.where(first_name: 'does not exist').take! +``` + #### `take!` `Model.take!` retrieves a record without any implicit ordering. For example: @@ -235,24 +248,6 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 `Model.last!` raises `ActiveRecord::RecordNotFound` if no matching record is found. -#### `find_by!` - -`Model.find_by!` finds the first record matching some conditions. It raises `ActiveRecord::RecordNotFound` if no matching record is found. For example: - -```ruby -Client.find_by! first_name: 'Lifo' -# => #<Client id: 1, first_name: "Lifo"> - -Client.find_by! first_name: 'Jon' -# => ActiveRecord::RecordNotFound -``` - -It is equivalent to writing: - -```ruby -Client.where(first_name: 'Lifo').take! -``` - ### Retrieving Multiple Objects #### Using Multiple Primary Keys |