From 13b3c77e393b8fb02588f39e6bfa10c832e251ff Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 30 Mar 2012 12:44:45 +0100 Subject: Add Relation#find_by and Relation#find_by! --- guides/source/active_record_querying.textile | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'guides') 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 Model.last returns +nil+ if no matching record is found. No exception will be raised. +h5. +find_by+ + +Model.find_by finds the first record matching some conditions. For example: + + +Client.find_by first_name: 'Lifo' +# => # + +Client.find_by first_name: 'Jon' +# => nil + + +It is equivalent to writing: + + +Client.where(first_name: 'Lifo').first + + h5(#first_1). +first!+ Model.first! finds the first record. For example: @@ -167,6 +185,24 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 Model.last! raises +RecordNotFound+ if no matching record is found. +h5(#find_by_1). +find_by!+ + +Model.find_by! finds the first record matching some conditions. It raises +RecordNotFound+ if no matching record is found. For example: + + +Client.find_by! first_name: 'Lifo' +# => # + +Client.find_by! first_name: 'Jon' +# => RecordNotFound + + +It is equivalent to writing: + + +Client.where(first_name: 'Lifo').first! + + h4. Retrieving Multiple Objects h5. Using Multiple Primary Keys -- cgit v1.2.3