Report abuse

add to model (can be in protected section)


			
def after_validation
  # Create an empty array to hold the field names
  fields = []
  # Iterate through the errors
  errors.each do |field,message|
    # Unless you have alreayd processed the field add the errors (This is necessary to remove duplicates for the 'has many' fields)
    unless fields.include?(field)
      # If the field of an error is really an association, then the 'validates_associated' found an error
      if self.class.reflect_on_all_associations.collect(&:name).index(field.to_sym)
        # Iterate through the objects in the association looking for the invalid ones
        self.send(field).each do |association|
          if association and !association.valid?
            # add the error messages of the associated object to my error messages
            association.errors.each_full do |msg|
              self.errors.add_to_base "#{association.class.name}: #{msg}"
            end
          end
          errors.delete field
        end
        fields << field
      end
    end
  end
end

add to config/initializers/error_patches.rb


			
class ActiveRecord::Errors
  def delete(attribute)
    @errors.delete attribute.to_s
  end
end