Mind Dump, Tech And Life Blog
written by Ivan Alenko
published under license CC4-BY
posted at 22. Oct '25

Howto Force Change Validation Message

This post will show how to change a validation message in code for uniqueness validator in a ActiveRecord model. A regular approach is to set message in YAML translation file or set a mesage in validates command. That is not always possible if a model is defined in a Rails Engine or a different gem (no majestic monoliths).

  # put this directly into a model class/monkey patch where validate statemens are, after them
  devise_uniq_validator = validators.find{|item| item.is_a?(ActiveRecord::Validations::UniquenessValidator) && item.attributes == [:email]}
  devise_uniq_validator.instance_variable_set(
    :@options,
    devise_uniq_validator.options.deep_dup.merge(:message => 'OMG! Email is not unique!').freeze,
  # )

Do not use this regularly, should be used only for special cases.

Add Comment