查询拿到一个表名,本以为可以直接使用,发现是报错的:
NoMethodError: undefined method `all' for "Organization":String
然后发现取到的表名是String
,于是查询变成了 "Organization".all
经过查询发现这样是可以的
"Organization".constantize.all
一个文档里面这么写:
https://api.rubyonrails.org/classes/String.html#method-i-constantize
constantize
tries to find a declared constant with the name
specified in the string. It raises a NameError when the name is not
in CamelCase or is not initialized. See
ActiveSupport::Inflector#constantize