title: Rails validate uniqueness fails on concurrent inputs
body: <p>I have a <code>Ticket</code> model with basic uniqueness validation on a string attribute <code>ticket_number</code> </p>
  
  <pre><code>validates :ticket_number, uniqueness: true
  </code></pre>
  
  <p>Here is the input code for the <code>ticket_number</code></p>
  
  <pre><code>&lt;%= f.number_field :ticket_number, :value =&gt; @ticket.ticket_number || (Ticket.exists? ? Ticket.maximum(:ticket_number).next) : 1 , :class =&gt; 'form-control' %&gt;
  </code></pre>
  
  <p>The validation seems to work as expected, however when we deployed the app and probably more than one users were entering <code>tickets</code> at the same time, which shouldn't cause an issue (right?) we found duplicated records (same ticket number) in the database(MySQL).</p>
  
  <p>It only happened once in a total of 600+ tickets so far but why and how to prevent that from happening ?</p>