Getting model class by string


#1

Hello,

I have an interessting problem, I’m not sure how to solve :slight_smile:
I have the name of a model in a variable like this:
My model is called Article
My var contains “Article”

Now - How do I get the model class, so I can call Article.find,
Article.new etc … ?

Greetings,
Gitte W.


#2

On 1/6/06, Gitte W. removed_email_address@domain.invalid wrote:

Greetings,
Gitte W.

‘Article’.constantize


rick
http://techno-weenie.net


#3

Gitte W. wrote:

I have an interessting problem, I’m not sure how to solve :slight_smile:
I have the name of a model in a variable like this:
My model is called Article
My var contains “Article”

Now - How do I get the model class, so I can call Article.find,
Article.new etc … ?

eval(“Article”).find and eval(“Article”).new. I believe there’s at least
one other way, but it eludes me at the moment.

Do realize though, that this poses a big security hole if you pass
tainted data to eval. eval(“Article.destroy_all && Article”) is fairly
boring to run for example.


#4

Jakob L. Skjerning wrote:

eval(“Article”).find and eval(“Article”).new. I believe there’s at least
one other way, but it eludes me at the moment.

Do realize though, that this poses a big security hole if you pass
tainted data to eval. eval(“Article.destroy_all && Article”) is fairly
boring to run for example.

I’m aware of the security issues. But I won’t call .destroy or anything

  • and the classes are taking from a db and evaluated before. It’s just
    for some lookup methods in my model.

Thank you very much for the help.

Greetings,
Gitte W.