I have a string, containing an Class name. It is, for example, a string containing "Article". That string came up from the params[]. What should I do to work with this string as if it was a class name? For instance, I want to do:
Article.all
and so on.
Any idea?
Assumes there really is a class with the name provided...
In ActiveSupport, there was String#constantize, which did the same thing, but I believe it's deprecated after 2.1.
EDIT: this is the implementation of constantize from ActiveSupport 2.1.2:
This solution is better than eval as you are evaluating params hash that might be manipulated by the user and could contain harmful actions. As a general rule: Never evaluate user input directly, that's a big security hole.
Update - a better version that works with namespaces:
I am not sure whether I understand your intention correctly. Here I assume
all
is an Class method ofArticle
andall
return an array of articles.