434

How can I get the class name from an ActiveRecord object?

I have:

result = User.find(1)

I tried:

result.class
# => User(id: integer, name: string ...)
result.to_s
# => #<User:0x3d07cdc>"

I need only the class name, in a string (User in this case). Is there a method for that?

I know this is pretty basic, but I searched both Rails' and Ruby's docs, and I couldn't find it.

1
  • 1
    @Oliver N.: With normal Ruby objects, Object#class.inspect gives the same as Object#class.name, whereas this isn't the case with ActiveRecord objects. Aug 9, 2011 at 23:53

6 Answers 6

814

You want to call .name on the object's class:

result.class.name
6
  • 6
    When I do this I get the Module names before it, so "Module::SubModule::Class", is there a way of getting just "Class" Sep 2, 2011 at 11:35
  • 29
    @Abe: result.class.name.split('::').last Oct 1, 2011 at 10:40
  • 97
    @Abe: even cleaner (ActiveSupport): result.class.name.demodulize
    – pseidemann
    Oct 30, 2011 at 20:48
  • 3
    For the newcomers out there, you can also obtain the class name as a string by using the class like this: User.name. User.to_s also seems to work. Nov 20, 2012 at 20:17
  • 1
    The demodulize method comes from here: apidock.com/rails/ActiveSupport/Inflector/demodulize (You need to load the ActiveSupport string inflections to be able to use if, if you are not a Rails project.) Jul 12, 2016 at 8:24
136

Here's the correct answer, extracted from comments by Daniel Rikowski and pseidemann. I'm tired of having to weed through comments to find the right answer...

If you use Rails (ActiveSupport):

result.class.name.demodulize

If you use POR (plain-ol-Ruby):

result.class.name.split('::').last
2
  • 4
    An answer that is comparing a solution to others and claims to better, should also explain why.
    – Dennis
    Aug 26, 2020 at 15:59
  • well, technically, my comment answered more than what was originally asked ;)
    – pseidemann
    Aug 19, 2022 at 12:14
38

Both result.class.to_s and result.class.name work.

1
  • 42
    But conceptually, #name returns the name, #to_s returns a string representation, which just happens to be identical to the name. I'd stick to using #name, just out of anal-retentiveness.
    – kch
    May 5, 2009 at 20:54
12

If you want to get a class name from inside a class method, class.name or self.class.name won't work. These will just output Class, since the class of a class is Class. Instead, you can just use name:

module Foo
  class Bar
    def self.say_name
      puts "I'm a #{name}!"
    end
  end
end

Foo::Bar.say_name

output:

I'm a Foo::Bar!
3

In Ruby, you'd use the object.class.name method as follows.

module Bank
  class Account
  end
end

irb(main):005:0> account = Bank::Account.new
=> #<Bank::Account:0x0000000106115b00>
irb(main):006:0> account.class.name
=> "Bank::Account"
irb(main):008:0> account.class.name.split("::").last
=> "Account"

In Rails, as others mentioned, you can use the demodulize method on a string, which is added by Active Support. It removes the module part from the constant expression in the string.

irb(main):014:0> account.class.name.demodulize
=> "Account"

Internally, this method calls the demodulize class method on the ActiveSupport::Inflector class, passing itself as the argument.

# File activesupport/lib/active_support/core_ext/string/inflections.rb, line 166
def demodulize
  ActiveSupport::Inflector.demodulize(self)
end

The Inflector.demodulize function does the same thing.

demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
demodulize('Inflections')                           # => "Inflections"
demodulize('::Inflections')                         # => "Inflections"
demodulize('')                                      # => ""

However, its internal implementation is different than the simple version above.

# File activesupport/lib/active_support/inflector/methods.rb, line 228
def demodulize(path)
  path = path.to_s
  if i = path.rindex("::")
    path[(i + 2)..-1]
  else
    path
  end
end

After converting the path argument to a string, it gets the index of the last occurrence of :: using Ruby's rindex function. If it exists, then it returns the remaining substring. Otherwise, it returns the original string. The array[n..-1] expression returns the substring from n to the last character in the string.

Now I haven't done any benchmark studies to find why Rails uses this alternative approach using rindex (please comment if you know why), but as a code readability enthusiast, I like the first one using the split and last functions.

Source: How to Get an Object's Class Name in Rails

2

In my case when I use something like result.class.name I got something like Module1::class_name. But if we only want class_name, use

result.class.table_name.singularize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.