title: Handling Unique Record Exceptions in a Controller
body: <p>I have a model called Subscription that has a unique index on the fields [:email, :location]. This means one email address can subscribe per location.</p>
  
  <p>In my model:</p>
  
  <pre><code>class Subscription &lt; ActiveRecord::Base
    validates :email, :presence =&gt; true, :uniqueness =&gt; true, :email_format =&gt; true, :uniqueness =&gt; {:scope =&gt; :location}
  end
  </code></pre>
  
  <p>In my create method. I want to handle the the exception <code>ActiveRecord::RecordNotUnique</code> differently than a regular error. How would I add that in to this generic create method?</p>
  
  <pre><code>  def create
      @subscription = Subscription.new(params[:subscription])
      respond_to do |format|
        if @subscription.save
          format.html { redirect_to(root_url, :notice =&gt; 'Subscription was successfully created.') }
        else
          format.html { render :action =&gt; 'new' }
        end
      end
    end
  </code></pre>