lib/rails_devs_for_data_integrity.rb
def handle_data_integrity_error(exception, record=nil, &block)
    @duplicate_exception = exception if exception.to_s =~ /^Mysql::Error: Duplicate entry/
    @foreign_key_exception = exception if exception.to_s =~ /^Mysql::Error.*foreign key constraint fails /

    if @duplicate_exception || @foreign_key_exception
      if block
        yield
      elsif record
        record.add_unique_key_error(exception)   if @duplicate_exception
        record.add_foreign_key_error(exception)  if @foreign_key_exception
        record
      end
    else
      raise exception
    end
  end
# If +exception+ is a unique key violation or a foreign key error,
# excute the block if it exists. If not and a record exists, add the
# appropriate error messages. Reraise any exceptions that are not data integrity violation errors
# Sometimes better to use +execute_with_data_integrity_check+ block
#
# +exception+ - Exception thrown from save (insert or update)
# +record+ - The activerecord object to add errors
#
# ===Example
#
# def save_safe
#   record = self
#   save
# rescue ActiveRecord::StatementInvalid => exception
#   handle_data_integrity_error(exception, record)
# end
If +exception+ is a unique key violation or a foreign key error,
excute the block if it exists. If not and a record exists, add the
appropriate error messages. Reraise any exceptions that are not data integrity violation errors
Sometimes better to use +execute_with_data_integrity_check+ block

+exception+ - Exception thrown from save (insert or update)
+record+ - The activerecord object to add errors

Example


If + exception + is a unique key violation or a foreign key error excute the block if it exists . If not and a record exists add the appropriate error messages . Reraise any exceptions that are not data integrity violation errors Sometimes better to use + execute_with_data_integrity_check + block + exception + - Exception thrown from save ( insert or update ) + record + - The activerecord object to add errors Example
def handle_data_integrity_error(exception, record=nil, &block)
    @duplicate_exception = exception if exception.to_s =~ /^Mysql::Error: Duplicate entry/
    @foreign_key_exception = exception if exception.to_s =~ /^Mysql::Error.*foreign key constraint fails /
    if @duplicate_exception || @foreign_key_exception
      if block
        yield
      elsif record
        record.add_unique_key_error(exception)   if @duplicate_exception
        record.add_foreign_key_error(exception)  if @foreign_key_exception
        record
      end
    else
      raise exception
    end
  end
def handle_data_integrity_error ( exception , record = nil , & block ) @duplicate_exception = exception if exception . to_s =~ / ^Mysql::Error: Duplicate entry / @foreign_key_exception = exception if exception . to_s =~ / ^Mysql::Error.*foreign key constraint fails  / if @duplicate_exception || @foreign_key_exception if block yield elsif record record . add_unique_key_error ( exception ) if @duplicate_exception record . add_foreign_key_error ( exception ) if @foreign_key_exception record end else raise exception end end
If +exception+ is a unique key violation or a foreign key error,
excute the block if it exists.
If + exception + is a unique key violation or a foreign key error excute the block if it exists .
exception
record
block