Skip to content

Commit

Permalink
Minor documentation enhancements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrés Mejía committed Aug 30, 2011
1 parent 9f03601 commit 302e57d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions railties/guides/source/active_record_querying.textile
Expand Up @@ -1069,9 +1069,7 @@ NOTE: Be sure to check the extensive *Active Record Validations and Callbacks Gu

h3. First or new

The +first_or_new+ method will work just like the +first_or_create+ but it will not call the +create+ action but the +new+ one.

This means that a new model instance will be created in memory but it won't be saved in the database. Continuing with the +first_or_create+ example, we now want the client named 'Nick':
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':

<ruby>
nick = Client.where(:first_name => 'Nick').first_or_new(:locked => false)
Expand All @@ -1084,12 +1082,19 @@ nick.new_record?
# => true
</ruby>

Because the object is not stored in the database, the SQL generated looks like this:
Because the object is not yet stored in the database, the SQL generated looks like this:

<sql>
SELECT * FROM clients WHERE (clients.first_name = 'Nick') LIMIT 1
</sql>

When you want to save it to the database, just call +save+:
nick.save
# => true
<ruby>

</ruby>

Just like you can use *+build+* instead of *+new+*, you can use *+first_or_build+* instead of *+first_or_new+*.

h3. Finding by SQL
Expand Down

0 comments on commit 302e57d

Please sign in to comment.