Permalink
Browse files

[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.
  • Loading branch information...
1 parent caa25e9 commit 7d9c3ff55c288d277b20c69df4a61c505a99f90c @schneems schneems committed Jun 28, 2014
Showing with 14 additions and 19 deletions.
  1. +14 −19 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

0 comments on commit 7d9c3ff

Please sign in to comment.