# TypeCast

**URL:** <https://www.ruby-forum.com/t/typecast/197268>\
**Category:** Rails\
**Created:** [March 3, 2008, 10:39am UTC](https://www.ruby-forum.com/t/typecast/197268 "2008-03-03T10:39:32Z")\
**Posts on this page:** 5\
**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/197268/1 "2008-03-03T10:39:32Z")

</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:44am UTC](https://www.ruby-forum.com/t/typecast/197268/2 "2008-03-03T10:44:12Z")

</div>

On 3 Mar 2008, at 09:39, Sijo Kg wrote:

> string  
> “ServiceDesk” which is the name of an active record class.

Could you not be writing model = ServiceDesk, in which case the  
problem completely goes away?  
If not you can use model = “ServiceDesk”.constantize

Fred

---

<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:52am UTC](https://www.ruby-forum.com/t/typecast/197268/3 "2008-03-03T10:52:57Z")

</div>

Hi  
Thanks for your reply.It worked

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, 7:27pm UTC](https://www.ruby-forum.com/t/typecast/197268/4 "2008-03-03T19:27:03Z")

</div>

On Mar 3, 2008, at 1:43 AM, Frederick C. wrote:

> Could you not be writing model = ServiceDesk, in which case the  
> problem completely goes away?  
> If not you can use model = “ServiceDesk”.constantize

Why not:

ServiceDesk.find(29)

What role does the string play in this?

---

<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 4, 2008, 4:41am UTC](https://www.ruby-forum.com/t/typecast/197268/5 "2008-03-04T04:41:33Z")

</div>

Because in model=“ServiceDesk”

“ServiceDesk” is one of the fiels of a table of type String.And there is  
also other entries in that field like Incident,etc.All are names of  
ActiveRecord modelclasses

Sijo
