aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-03-30 12:44:45 +0100
committerJon Leighton <j@jonathanleighton.com>2012-03-30 12:52:29 +0100
commit13b3c77e393b8fb02588f39e6bfa10c832e251ff (patch)
tree01bbb86cd94d6f18060d70c8a2d7ec8bd85ed0a4 /guides
parent3a8c54396ea3965eb7601501d7bb9618ff305728 (diff)
downloadrails-13b3c77e393b8fb02588f39e6bfa10c832e251ff.tar.gz
rails-13b3c77e393b8fb02588f39e6bfa10c832e251ff.tar.bz2
rails-13b3c77e393b8fb02588f39e6bfa10c832e251ff.zip
Add Relation#find_by and Relation#find_by!
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_querying.textile36
1 files changed, 36 insertions, 0 deletions
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile
index 8e23a577e2..8471d67def 100644
--- a/guides/source/active_record_querying.textile
+++ b/guides/source/active_record_querying.textile
@@ -133,6 +133,24 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
<tt>Model.last</tt> returns +nil+ if no matching record is found. No exception will be raised.
+h5. +find_by+
+
+<tt>Model.find_by</tt> finds the first record matching some conditions. For example:
+
+<ruby>
+Client.find_by first_name: 'Lifo'
+# => #<Client id: 1, first_name: "Lifo">
+
+Client.find_by first_name: 'Jon'
+# => nil
+</ruby>
+
+It is equivalent to writing:
+
+<ruby>
+Client.where(first_name: 'Lifo').first
+</ruby>
+
h5(#first_1). +first!+
<tt>Model.first!</tt> finds the first record. For example:
@@ -167,6 +185,24 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
<tt>Model.last!</tt> raises +RecordNotFound+ if no matching record is found.
+h5(#find_by_1). +find_by!+
+
+<tt>Model.find_by!</tt> finds the first record matching some conditions. It raises +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'
+# => RecordNotFound
+</ruby>
+
+It is equivalent to writing:
+
+<ruby>
+Client.where(first_name: 'Lifo').first!
+</ruby>
+
h4. Retrieving Multiple Objects
h5. Using Multiple Primary Keys