views:

103

answers:

4

I assume this is really easy, and I'm missing something obvious. I'm connecting to a legacy database that has column dblOrderQty. I'm validating it thusly:

validates_numericality_of :dblOrderQty, :greater_than => 0

This, of course, presents "Dblorderqty must be greater than 0". I'd much rather have that say "Quantity must be greater than 0," but I can't find a way to modify the column name in the message.

+1  A: 

Not sure if this is best way, but it works :)

class ModelName < ActiveRecord::Base
  HUMANIZED_ATTRIBUTES = {
    :dblOrderQty => "Order Quantity"
  }

  validates_numericality_of :dblOrderQty, :greater_than => 0

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


Rishav Rastogi
+1  A: 

I haven't tested this but you could possibly overwrite your default attribute accessor in your model like this

def quantity
  read_attribute(:dblOrderQty)
end

And then refer to the overwritten attribute. As alway, refer to the docs

Matthew
A: 

I use the error_messages_for plugin by Bob Silva. http://agilewebdevelopment.com/plugins/enhanced%5Factiverecord%5Ferrors

It includes ability to pass in a hash to change the names of some or all of the model's attributes in the error message. This is done in the view (or a helper). Over-writing the attribute names is also important for localized apps.

Larry K
+2  A: 

I think it'd be cleaner to use localization (as Larry K implied) by adding the following to config/locales/en.yml:

en:
  activerecord:
    attributes:
      model_name:
        dblOrderQty: "Quantity"

I'm not sure if the CamelCase will confuse it or not.

eremite