Question: ❓❓❓


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.


Good Answer: 👌👌👌

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

result.class.name

Read More: 👉 https://stackoverflow.com/questions/826210/how-do-i-get-the-name-of-a-ruby-class