title: Rails - force field uppercase and validate uniquely
body: <p>Airports have four-letter ICAO codes. By convention, these are always uppercase. I'm creating a form for receiving user input, but this form needs to be able to accept user input in mixed case, and prevent them from creating dupes.</p>
  
  <p>The default <code>:uniqueness</code> is case-sensitive, of course. I figured out how to transform the user's input to uppercase before it gets saved, but the problem is that this appears to be post-validation, instead of pre-validation.</p>
  
  <p>For example, if there is already an Airport with ICAO of <code>KLAX</code>, a user can enter <code>klax</code>, it will get validated as unique, and then transformed to uppercase and stored, resulting in duplicates.</p>
  
  <p>Here's my model code at present.</p>
  
  <pre><code>class Airport &lt; ActiveRecord::Base
    validates :icao, :name, :lat, :lon, :presence =&gt; true
    validates :icao, :uniqueness =&gt; true
  
    before_save :uppercase_icao
  
    def uppercase_icao
      icao.upcase!
    end
  end
  </code></pre>