From a9668680fd5eda2797f003a710eaf220768d95da Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sat, 5 May 2012 23:54:16 +0530 Subject: some corrections in the AR query guide [ci skip] --- guides/source/active_record_querying.textile | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'guides') diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index d16cdd66ee..a9cb424eaa 100644 --- a/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -101,7 +101,7 @@ SELECT * FROM clients WHERE (clients.id = 10) LIMIT 1 h5. +take+ -Model.take retrieves a record without any implicit ordering. The retrieved record may vary depending on the database engine. For example: +Model.take retrieves a record without any implicit ordering. For example: client = Client.take @@ -114,11 +114,13 @@ The SQL equivalent of the above is: SELECT * FROM clients LIMIT 1 -Model.take returns +nil+ if no record is found. No exception will be raised. +Model.take returns +nil+ if no record is found and no exception will be raised. + +TIP: The retrieved record may vary depending on the database engine. h5. +first+ -Model.first finds the first record. If no order is chained it will order by primary key. For example: +Model.first finds the first record ordered by the primary key. For example: client = Client.first @@ -131,11 +133,11 @@ The SQL equivalent of the above is: SELECT * FROM clients ORDER BY clients.id ASC LIMIT 1 -Model.first returns +nil+ if no matching record is found. No exception will be raised. +Model.first returns +nil+ if no matching record is found and no exception will be raised. h5. +last+ -Model.last finds the last record. If no order is chained it will order by primary key. For example: +Model.last finds the last record ordered by the primary key. For example: client = Client.last @@ -148,7 +150,7 @@ The SQL equivalent of the above is: 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. +Model.last returns +nil+ if no matching record is found and no exception will be raised. h5. +find_by+ @@ -183,11 +185,11 @@ The SQL equivalent of the above is: SELECT * FROM clients LIMIT 1 -Model.take! raises +RecordNotFound+ if no matching record is found. +Model.take! raises +ActiveRecord::RecordNotFound+ if no matching record is found. h5(#first_1). +first!+ -Model.first! finds the first record. If no order is chained it will order by primary key. For example: +Model.first! finds the first record ordered by the primary key. For example: client = Client.first! @@ -200,11 +202,11 @@ The SQL equivalent of the above is: SELECT * FROM clients ORDER BY clients.id ASC LIMIT 1 -Model.first! raises +RecordNotFound+ if no matching record is found. +Model.first! raises +ActiveRecord::RecordNotFound+ if no matching record is found. h5(#last_1). +last!+ -Model.last! finds the last record. If no order is chained it will order by primary key. For example: +Model.last! finds the last record ordered by the primary key. For example: client = Client.last! @@ -217,18 +219,18 @@ The SQL equivalent of the above is: SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 -Model.last! raises +RecordNotFound+ if no matching record is found. +Model.last! raises +ActiveRecord::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: +Model.find_by! finds the first record matching some conditions. It raises +ActiveRecord::RecordNotFound+ if no matching record is found. For example: Client.find_by! first_name: 'Lifo' # => # Client.find_by! first_name: 'Jon' -# => RecordNotFound +# => ActiveRecord::RecordNotFound It is equivalent to writing: -- cgit v1.2.3