Browse files

Fixes in the guide

  • Loading branch information...
1 parent 915ae48 commit 9acaed99d54cc07909468798afff8cba7cd061c5 @andmej committed Aug 30, 2011
Showing with 5 additions and 5 deletions.
  1. +5 −5 railties/guides/source/active_record_querying.textile
@@ -1018,7 +1018,7 @@ If you want to find both by name and locked, you can chain these finders togethe
WARNING: Up to and including Rails 3.1, when the number of arguments passed to a dynamic finder method is lesser than the number of fields, say <tt>Client.find_by_name_and_locked("Ryan")</tt>, the behavior is to pass +nil+ as the missing argument. This is *unintentional* and this behavior will be changed in Rails 3.2 to throw an +ArgumentError+.
-h3. Find or create
+h3. Find or build a new object
It's common that you need to find a record or create it if it doesn't exist. You can do that with the +first_or_create+ and +first_or_create!+ methods.
@@ -1067,7 +1067,7 @@ Client.where(:first_name => 'Andy').first_or_create!(:locked => false)
NOTE: Be sure to check the extensive *Active Record Validations and Callbacks Guide* for more information about validations.
-h3. First or new
+h4. +first_or_new+
The +first_or_new+ method will work just like +first_or_create+ but it will not call +create+ but the +new+. This means that a new model instance will be created in memory but won't be saved to the database. Continuing with the +first_or_create+ example, we now want the client named 'Nick':
@@ -1089,10 +1089,10 @@ SELECT * FROM clients WHERE (clients.first_name = 'Nick') LIMIT 1
When you want to save it to the database, just call +save+:
-# => true
+# => true
Just like you can use *+build+* instead of *+new+*, you can use *+first_or_build+* instead of *+first_or_new+*.

0 comments on commit 9acaed9

Please sign in to comment.