# How to convert a string into class name?

**URL:** <https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739>\
**Category:** Rails\
**Created:** [January 12, 2007, 10:56am UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739 "2007-01-12T10:56:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 12, 2007, 10:56am UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/1 "2007-01-12T10:56:39Z")

</div>

I transfer a class name information (‘model’, a string) using a URL  
params :id.  
and I get the name using params[:id] in an controller action.  
Now, how can I convert ‘model’ (string) into Model (class name) to  
perform a Model.find(X)?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 12, 2007, 11:46am UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/2 "2007-01-12T11:46:16Z")

</div>

Nanyang Z. wrote:

> I transfer a class name information (‘model’, a string) using a URL  
> params :id.  
> and I get the name using params[:id] in an controller action.  
> Now, how can I convert ‘model’ (string) into Model (class name) to  
> perform a Model.find(X)?

Try this:

params[:id].camelize.constantize

This will turn the string “my\_super\_cool\_model” into the constant  
(ie. the class) MySuperCoolModel.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 12, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/3 "2007-01-12T12:37:24Z")

</div>

Snowman wrote:

> Try this:
> 
> params[:id].camelize.constantize
> 
> This will turn the string “my\_super\_cool\_model” into the constant  
> (ie. the class) MySuperCoolModel.

it works! thanks.  
btw, what’s the difference between .camelize and .classify?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 12, 2007, 12:58pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/4 "2007-01-12T12:58:17Z")

</div>

> btw, what’s the difference between .camelize and .classify?

You use classify when you’re starting with the plural version (eg. the  
table name). So “my\_super\_cool\_models”.classify =\> “MySuperCoolModel”

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 12, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/5 "2007-01-12T13:32:56Z")

</div>

instance\_variable\_get("@#{object}").send(attribute)

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 12, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/6 "2007-01-12T13:23:07Z")

</div>

I see. Thanks.

Now I know how to convert a string into a relative class name, like I  
can make ‘user’ to User.

But what if I know ‘object’ (string) and ‘attribute’(string) when I want  
to get a @object.attribute?  
I’ll use this in helper method.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [July 28, 2007, 1:56am UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/7 "2007-07-28T01:56:25Z")

</div>

Sorry to revive such an old post, but I saw this and had to mention  
something…

Snow Man wrote:

> > btw, what’s the difference between .camelize and .classify?
> 
> You use classify when you’re starting with the plural version (eg. the  
> table name). So “my\_super\_cool\_models”.classify =\> “MySuperCoolModel”

While that’s true, classify is sometimes incorrect. Example:

> > ‘email\_address’.classify  
> > =\> “EmailAddres”

What I would suggest if you have the plural form, or if you’re not sure  
what form you have, is to call singularize first, then camelize.  
Example:

> > ‘email\_addresses’.singularize.camelize  
> > =\> “EmailAddress”

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 12, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-into-class-name/81739/8 "2007-01-12T13:40:58Z")

</div>

Snow Man wrote:

> instance\_variable\_get("@#{object}").send(attribute)

Thanks!
