查詢拿到一個表名,本以爲可以直接使用,發現是報錯的:
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