"ModelName".constantize # -> ModelName
Rails3から導入された constantizeメソッドは、文字列が現在のネームスペース内で定数として存在していないか探し、存在していたらそれを返します。
クラスは定数なので、もしActiveRecordを継承して定義されたクラスの場合、そのままDBのレコードにアクセスすることもできます。
"ActiveRecordModelName".constantize.first # -> DB recordが返ってきます
使いどき
あるRailsプロジェクト外のDBから、データをマイグレートしているときに、マイグレート元とマイグレート後で、スキーマを結構変えました。
そこで、DatabaseConvertMapperというクラスを自作して、そこでDBのマイグレーションの対応表を作っていました。
その文字列からレコードにアクセスするときに使ってみました。他にも良い方法あればぜひ教えてください。