title: self.errors[:base] << inside an ActiveRecord transaction block
body: <p>I want to update @some_instance unless the user does not meet some criteria. I put the criteria check and the @some_instance.save! in a transaction block so that if either of them fail, no transaction is made. This works well, but I am having trouble returning the correct error message. If the user does not meet the criteria, I want to return the reason why, OR if the @some_instance doesn't save, I want to return that error.</p>
  
  <p>My code:</p>
  
  <pre><code>#some_controller.rb
  begin
    Some_Class.transaction do
      return render json: { error: @user.errors }, status: :payment_required, 
        location: new_payment_path unless @user.meets_criteria
      @some_instance.save!
    end
  rescue ActiveRecord::RecordInvalid =&gt; exception
    render :json =&gt; { :error =&gt; exception.messages }, status: :unprocessable_entity
  rescue =&gt; error
    # handle some other exception
  end
  
  #User.rb
  def meets_criteria
    self.errors[:base] &lt;&lt; "Does not meet criteria"
    return false
  end
  </code></pre>
  
  <p>The problem I'm facing is this:  When the meets_criteria method returns false, I expect the return render json line to execute. Instead it catches an error in "rescue => error".</p>
  
  <p>The return render json is never executed.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>@Gen suggested using a before_action instead of calling the meets_criteria in the transaction do block. I think this is a much better implementation, however I'm still curious why the return render is never called. Is it because ActiveRecord raises an error? If so shouldn't that be caught in the RecordInvalid exception?</p>