How to get human readable class name in Ruby on Rails?

Soundar Rathinasamy picture Soundar Rathinasamy · Sep 10, 2012 · Viewed 9.6k times · Source

I am building an application with Ruby 1.9.3 and Rails 3.0.9

I have a class like below.

module CDA
  class Document
    def humanize_class_name
      self.class.name.gsub("::","")
    end
  end
end

I want the class name like "CDADocument".

Is my humanize_class_name method is the correct way to achieve this?

OR

Any other built in methods available with Rails?

Answer

Max picture Max · Dec 11, 2012

If you using i18n you can call Model.model_name.human to get the localized model name.

E.g.: Event.model_name.human returns my localized name.

Docs: http://api.rubyonrails.org/classes/ActiveModel/Name.html#method-i-human