Successful safe constantize
suggest changeUser is an ActiveRecord or Mongoid class. Replace User with a class in your project. Ruby classes such as Integer and Array work too.
my_string = "User" # Capitalized string
# => 'User'
my_constant = my_string.safe_constantize
# => User
my_constant.all.count
# => 18
my_string = "Array"
# => 'Array'
my_constant = my_string.safe_constantize
# => Array
my_constant.new(4)
# => [nil, nil, nil, nil]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents