The Pug Automatic

Change displayed column name in Rails validation messages

Written December 3, 2007. Tagged Ruby, Ruby on Rails.

I recently wanted to change how a database column name is represented in validation messages. Case in point: I wanted the "email" column to give error messages like "E-mail address must be valid".

I poked through the Rails code, and it turns out this is very simple. The method seems underdocumented/underblogged though, so I thought I'd write it up.

What you do is define a human_attribute_name method on the model class. The method is passed the column name as a string and returns the string to use in validation messages. You could exploit this in a couple of different ways, but this is what I do:

class User < ActiveRecord::Base

:email => "E-mail address"

def self.human_attribute_name(attr)
HUMANIZED_ATTRIBUTES[attr.to_sym] || super