# Typecast

**URL:** <https://www.ruby-forum.com/t/typecast/130782>\
**Category:** Ruby\
**Created:** [March 3, 2008, 10:39am UTC](https://www.ruby-forum.com/t/typecast/130782 "2008-03-03T10:39:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 3, 2008, 10:39am UTC](https://www.ruby-forum.com/t/typecast/130782/1 "2008-03-03T10:39:08Z")

</div>

Hi  
I have an ActiveRecord class ServiceDesk and a string “ServiceDesk”  
.How can I convert the string “ServiceDesk” to create a new object of  
the class ServiceDesk?

Concept is made clear below.

model = “ServiceDesk” // Now the variable, model, contains the string  
“ServiceDesk” which is the name of an active record class.

I am trying to achieve the following. how can I really achieve this…  
Thanks in advance.

model.find(29)

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 3, 2008, 10:48am UTC](https://www.ruby-forum.com/t/typecast/130782/2 "2008-03-03T10:48:21Z")

</div>

On 3/3/08, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Kernel.const\_get(model)

Christopher

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 3, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/typecast/130782/3 "2008-03-03T12:49:11Z")

</div>

Hi,

On Mon, Mar 3, 2008 at 8:47 PM, Christopher S. \<  
removed\_email\_address@domain.invalid\> wrote:

> Kernel.const\_get(model)

And hence Kernel.const\_get(model).new.

Arlen

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 3, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/typecast/130782/4 "2008-03-03T23:33:45Z")

</div>

On Mon, Mar 3, 2008 at 1:39 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have an ActiveRecord class ServiceDesk and a string “ServiceDesk”  
> .How can I convert the string “ServiceDesk” to create a new object of  
> the class ServiceDesk?

Since you’re working with Rails and therefore ActiveSupport, you can do:

“ServiceDesk”.constantize.new

~ j.
