問題:
How can I get the class name from an ActiveRecord object? 如何從ActiveRecord對象獲取類名?
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). 我只需要字符串中的類名(在本例中爲User
)。 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. 我知道這是非常基本的,但我搜索了Rails和Ruby的文檔,我找不到它。
解決方案:
參考一: https://stackoom.com/question/3Svy參考二: How do I get the name of a Ruby class?