# Getting model class by string

**URL:** <https://www.ruby-forum.com/t/getting-model-class-by-string/51751>\
**Category:** Rails\
**Created:** [January 6, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/getting-model-class-by-string/51751 "2006-01-06T18:36:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 6, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/getting-model-class-by-string/51751/1 "2006-01-06T18:36:33Z")

</div>

Hello,

I have an interessting problem, I’m not sure how to solve 🙂  
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.

---

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 6, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/getting-model-class-by-string/51751/2 "2006-01-06T18:57:38Z")

</div>

On 1/6/06, Gitte W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Greetings,  
> Gitte W.

‘Article’.constantize

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 6, 2006, 7:03pm UTC](https://www.ruby-forum.com/t/getting-model-class-by-string/51751/3 "2006-01-06T19:03:38Z")

</div>

Gitte W. wrote:

> I have an interessting problem, I’m not sure how to solve 🙂  
> 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.

---

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 7, 2006, 1:07am UTC](https://www.ruby-forum.com/t/getting-model-class-by-string/51751/4 "2006-01-07T01:07:41Z")

</div>

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.
