title: How to display custom error message for ActiveRecord::JDBCError in Ruby on Rails?
body: <p>I have a HTML table in my View to <code>create, update and delete</code> the records for a table called <code>Mast_Freq</code>.</p>
  
  <p>The <code>Primary key</code> of this table is referenced by foreign key <code>'MastFreq'</code> in table <code>'AssFreq'</code> and some other tables. So, When I try to delete some record in the <code>MastFreq</code> I got the error message like the following.</p>
  
  <pre><code>ActiveRecord::JDBCError: [Sybase][JDBC Driver][SQL Anywhere]Primary key for row in table 'MastFreq' is referenced by foreign key 'MastFreq' in table 'AssFreq': DELETE FROM "MastFreq" WHERE "MastFreq"."Frequency_Code" = 'A'
  </code></pre>
  
  <p>How can i display a custom error message to the user instead of this error message. This record should not be delete.</p>
  
  <p><code>Frequency_Code</code> is the primary key of the table <code>MastFreq</code>. </p>
  
  <pre><code>Controller:
  ----------
  class Asset::MastFreqsController &lt; AssetController
  
    rescue_from ActiveRecord::JDBCError, :with =&gt; :jdbc_error
  
    def destroy
      begin
        @asset_master_frequency = Asset::MastFreq.find(params[:id])
        result = @asset_master_frequency.destroy
  
        respond_to do |format|
      format.html{ redirect_to :action =&gt; :index}
      format.json{ render :json =&gt; result}
        end
      rescue ActiveRecord::JDBCError
      end
    end
  
    protected
    def jdbc_error(exception)
      flash[:error] = "You Cannot delete this Frequency Code" + exception.inspect
      redirect_to asset_master_frequencies_path
    end
  end
  </code></pre>