title: Handling Unique Validation with Status Accepted
body: <p>I am making Rails backend api for mobile app, and want to validate unique record with accepted status code.</p>
  
  <p>@Why?</p>
  
  <p>When I save entries from mobile app, I want to avoid saving duplicate records in Rails, but I also want to send "accepted status code" from Rails to mobile app instead of error status not to stop running bulk saving from mobile.</p>
  
  <p>Model</p>
  
  <pre><code>class Entry &lt; ActiveRecord::Base
    validates :uuid, uniqueness: true
  end
  </code></pre>
  
  <p>Controllers</p>
  
  <pre><code>  def create
  
      # Check if the same record already exists
      entry = Entry.find_by(uuid: entry_params[:uuid])
  
      if entry.present?
        render json: {errors: {message: 'The same uuid already exists.'}}, status: :accepted
        return
      end
  
      entry             = Entry.new
      entry.attributes  = entry_params
  
      if entry.save
        render json: entry, status: :created
      else
        render json: entry.errors, status: :unprocessable_entity
      end
    end
  </code></pre>
  
  <p>The above code works as I want, but I am wondering if I can use validations of Rails properly instead.</p>
  
  <p>This is what I tried, but couldn't get unique uuid error unfortunately. Is there any better way to handle this situation?</p>
  
  <p>ApplicationController</p>
  
  <pre><code>rescue_from ActiveRecord::RecordNotUnique, with: :record_not_unique
  
      def record_not_unique
        render json: {errors: {message: 'Record not unique.'}}, status: :accepted
      end
  </code></pre>