title: How do I destroy the other has_one relation when updating an association?
body: <p>I have a <code>User</code> which has_one <code>Widget</code>.</p>
  
  <pre><code>class User
    has_one :widget, :dependent =&gt; :destroy
  end
  
  class Widget
    belongs_to :user
  end
  </code></pre>
  
  <p>And when I create a new <code>Widget</code> for a <code>User</code>, I want to destroy the old one associated with the <code>User</code>. </p>
  
  <p>Here's my situation:</p>
  
  <p><strong>Create a user:</strong></p>
  
  <pre><code>user = User.new
  user.save
  user # =&gt; #&lt;User id: 1&gt;
  </code></pre>
  
  <p><strong>Create user's widget:</strong></p>
  
  <pre><code>widget = Widget.new
  widget.user = user
  widget.save
  </code></pre>
  
  <p><strong>Reload and check the widget:</strong></p>
  
  <pre><code>user.reload
  user.widget # =&gt; #&lt;Widget id: 1, user_id: 1&gt;
  </code></pre>
  
  <p><strong>Build a widget, notice that the existing widget is destroyed before the other is saved:</strong></p>
  
  <pre><code>user.build_widget # =&gt; #&lt;Widget id: nil, user_id: 1&gt;
  user.reload
  user.widget # =&gt; nil
  </code></pre>
  
  <p><strong>Recreate user's widget:</strong></p>
  
  <pre><code>user.create_widget # =&gt; #&lt;Widget id: 2, user_id: 1&gt;
  </code></pre>
  
  <p><strong>Create another widget:</strong></p>
  
  <pre><code>widget = Widget.new :user =&gt; user
  widget.save
  </code></pre>
  
  <p><strong>Now, both exist:</strong></p>
  
  <pre><code>Widget.find(2) # =&gt; #&lt;Widget id: 2, user_id: 1&gt;
  Widget.find(3) # =&gt; #&lt;Widget id: 3, user_id: 1&gt;
  </code></pre>
  
  <p><strong>And user's is the first:</strong></p>
  
  <pre><code>user.reload
  user.widget # =&gt; #&lt;Widget id: 2, user_id: 1&gt;
  </code></pre>
  
  <p><strong>Is there any way to do this:</strong></p>
  
  <pre><code>def create
    @widget = current_user.build_widget(params[:widget])
  
    respond_to do |format|
      if @widget.save
        format.html { redirect_to widget_path, notice: 'Widget was successfully created.' }
        format.json { render json: @widget, status: :created, location: @widget }
      else
        format.html { render action: 'new' }
        format.json { render json: @widget.errors, status: :unprocessable_entity }
      end
    end
  end
  </code></pre>
  
  <p><strong>without deleting the old widget before saving, or this:</strong></p>
  
  <pre><code>def create
    @widget = Widget.new(params[:widget])
    @widget.user = current_user
  
    respond_to do |format|
      if @widget.save
        format.html { redirect_to widget_path, notice: 'Widget was successfully created.' }
        format.json { render json: @widget, status: :created, location: @widget }
     else
        format.html { render action: 'new' }
        format.json { render json: @widget.errors, status: :unprocessable_entity }
      end
    end
  </code></pre>
  
  <p>end</p>
  
  <p><strong>without keeping two copies around?</strong></p>
  
  <p>I don't want to muck up my controllers with transactions like</p>
  
  <pre><code>Widget.transaction do
    old_widget.destroy
    new_widget.save
  end
  </code></pre>
  
  <p>but so far, this seems like the only way.</p>