To do this, you do not need to rename the table columns. There is a very clean fix:
class User < ActiveRecord::Base HUMAN_ATTRIBUTE_NAMES = { :email => 'Email address', :first_name => 'First name' } class << self def human_attribute_name attribute_name HUMAN_ATTRIBUTE_NAMES[attribute_name.to_sym] || super end end end
What we did was create an attribute hash where we want to configure the names. You do not need to list them all, as many attribute names will work out of the box the way you want. Then we will override the ActiveRecord method human_attribute_name to try to find the name in our hash first.
This does two very cool things: you no longer need to specify custom shortcuts in your forms, and your error messages will also have new names! As a bonus, you can use these names wherever you want by calling:
<%= User.human_attribute_name(:email) %>
This creates a more unified approach to naming. If you want to change “email” to “email” next week, you only need to do this in one place.
Hope this helps!
Jaime bellmyer
source share