User是ActiveRecord或Mongoid类。替换User为Rails项目中的任何类(甚至是Integer或Array)
my_string = "User" # 大写字符串 # => '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]