title: ActiveRecord find_or_initialize_by race conditions
body: <p>I have a scenario where 2 db connections might both run Model.find_or_initialize_by(params) and raise an error: <em>PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint</em></p>
  
  <p>I'd like to update my code so it could gracefully recover from it.  Something like:</p>
  
  <pre><code>record = nil
  
  begin
    record = Model.find_or_initialize_by(params)
  rescue ActiveRecord::RecordNotUnique
    record = Model.where(params).first
  end
  
  return record
  </code></pre>
  
  <p>The trouble is that there's not a nice/easy way to reproduce this on my local machine, so I'm not confident that my fix actually works.</p>
  
  <p>So I thought I'd get a bit creative and try calling create 2 times (locally) in a row which should raise then <em>PG::UniqueViolation: ERROR</em>, then I could rescue from it and make sure everything is handled gracefully.  </p>
  
  <p>But I get this error:  <em>PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block</em></p>
  
  <p>I get this error even when I wrap everything in individual transaction blocks</p>
  
  <pre><code>record = nil
  
  Model.transaction do
    record = Model.create(params)
  end
  
  begin
    Model.transaction do
      record = Model.create(params)
    end
  rescue ActiveRecord::RecordNotUnique
  end
  
  Model.transaction do
    record = Model.where(params).first
  end
  
  return record
  </code></pre>
  
  <p>My questions:</p>
  
  <ul>
  <li>What's the right way to gracefully handle the race condition I mentioned at the very beginning of this post?</li>
  <li>How do I test this locally?</li>
  </ul>
  
  <p>I imagine there's probably something simple that I'm missing here, but it's late and perhaps I'm not thinking too clearly.</p>
  
  <p>I'm running postgres 9.3 and rails 4.</p>
  
  <p><strong>EDIT</strong> Turns out that find_or_initialize_by should have been find_or_create_by and the errors I was getting was from the actual save call that happened later on in execution.  #VeryTiredWhenIWroteThis</p>