Custom ActiveRecord Error Message

I'm generating 3 models on 1 controller and i'm rescuing
ActiveRecord::RecordInvalid for validation errors, however 2 of the
models have the same attribute called "name", and if there's an error on
that field, I wouldn't know whether that belongs to model A or model B.

What then is the best way of modifying the error message of a validation
error without having to rely on locales which I think ActiveRecord
encourages you to do.

Currently, my solution is overriding the RecordInvalid#initialize
(adding "model class: #{record.class}" to the :errors hash value), but I
dont think this is a clean solution.

module ActiveRecord
  class RecordInvalid
    def initialize(record)
      @record = record
      errors = @record.errors.full_messages.join(
        I18n.t('support.array.words_connector', :default => ', ')
      )
      super(I18n.t('activerecord.errors.messages.record_invalid',
            :errors => "model class: #{record.class}: #{errors}"))
    end
  end
end

Reginald Tan wrote in post #1019523:

I'm generating 3 models on 1 controller and i'm rescuing
ActiveRecord::RecordInvalid for validation errors,

What are you doing in the rescue block? Why can't you do something like
this:

rescue ActiveRecord::RecordInvalid => e
  puts e.record.class
end

You're right. How come I didn't think of that. Thanks for the tip :smiley: