title: Ruby on Rails Tutorial -- strange uses of upcase/downcase and case-insensitivity
body: <p>Working my way through Michael Hartl's excellent <a href="http://ruby.railstutorial.org/ruby-on-rails-tutorial-book" rel="nofollow">tutorial</a> for Ruby on Rails. I'm at the point where he creates a test that checks for duplicate email addresses, and I'm a little confused about his use of upcase, downcase, and case-insensitive checks.</p>
  
  <p>The test (Listing 6.17) looks like this:</p>
  
  <pre><code>describe User do
    before do
      @user = User.new(name: "Example User", email: "user@example.com")
    end
    .
    .
    .
    describe "when email address is already taken" do
      before do
        user_with_same_email = @user.dup
        user_with_same_email.email = @user.email.upcase
        user_with_same_email.save
      end
  
      it { should_not be_valid }
    end
  end
  </code></pre>
  
  <p>Note the call to <code>upcase</code>. All fine. But in his validity check (6.18), he sets case sensitivity <strong><em>off</em></strong>.</p>
  
  <pre><code>validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }
  </code></pre>
  
  <p>What? Why did he convert the copy to uppercase if he was going to do a case-insensitive validation?</p>
  
  <p>Finally, in 6.20, he sets up a <code>before_save</code> block that converts a new user's email to lowercase.</p>
  
  <pre><code>before_save { self.email = email.downcase }
  </code></pre>
  
  <p>That makes perfect sense, because you want lowercase in your database. But I'm confused as to why he used uppercase in the test, given that the save is going to convert the email address to lowercase anyway. Am I missing something obvious?</p>