Permalink
Browse files

Add notes about upsert in documentation. [ mongoid/mongoid#2220 ]

  • Loading branch information...
1 parent eac28a4 commit 7d924337bb86c38ae54cb9c1d84015b4e146d977 @durran durran committed Jul 22, 2012
Showing with 30 additions and 2 deletions.
  1. +3 −0 en/mongoid/docs/callbacks.html
  2. +27 −2 en/mongoid/docs/persistence.html
@@ -104,6 +104,9 @@
<li><code>before_update</code></li>
<li><code>around_update</code></li>
<li><code>after_update</code></li>
+ <li><code>before_upsert</code></li>
+ <li><code>around_upsert</code></li>
+ <li><code>after_upsert</code></li>
<li><code>before_save</code></li>
<li><code>around_save</code></li>
<li><code>after_save</code></li>
@@ -178,8 +178,8 @@
<p class='doc'>
<i>
Saves the changed attributes to the database
- atomically, or insert the document if new. Can
- bypass validations if wanted.
+ atomically, or insert the document if flagged as a new_record via
+ <code>Model#rew_record?</code>. Can bypass validations if wanted.
</i>
</p>
</td>
@@ -278,6 +278,31 @@
</tr>
<tr>
<td>
+ <code>Model#upsert</code>
+ <p class='doc'>
+ <i>
+ Performs a MongoDB <code>upsert</code> on the document. If the
+ document exists in the database, it will get overwritten with
+ the current attributes of the document in memory. If the document
+ does not exist in the database, it will be inserted. Note that
+ this only runs the <code>{before|after|around}_upsert</code>
+ callbacks.
+ </i>
+ </p>
+ </td>
+ <td>
+ <div class="CodeRay">
+ <div class="code"><pre>person = <span class="constant">Person</span>.new(&#x000A; first_name: <span class="string"><span class="delimiter">&quot;</span><span class="content">Heinrich</span><span class="delimiter">&quot;</span></span>,&#x000A; last_name: <span class="string"><span class="delimiter">&quot;</span><span class="content">Heine</span><span class="delimiter">&quot;</span></span>&#x000A;)&#x000A;person.upsert</pre></div>
+ </div>
+ </td>
+ <td>
+ <div class="CodeRay">
+ <div class="code"><pre>collections[<span class="symbol">:people</span>].upsert({&#x000A; first_name: <span class="string"><span class="delimiter">&quot;</span><span class="content">Heinrich</span><span class="delimiter">&quot;</span></span>,&#x000A; last_name: <span class="string"><span class="delimiter">&quot;</span><span class="content">Heine</span><span class="delimiter">&quot;</span></span>&#x000A;})</pre></div>
+ </div>
+ </td>
+ </tr>
+ <tr>
+ <td>
<code>Model#touch</code>
<p class='doc'>
<i>

0 comments on commit 7d92433

Please sign in to comment.