Typecast

S

Sijo Kg

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
 
C

Christopher Swasey

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.

Kernel.const_get(model)

Christopher
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Mon, Mar 3, 2008 at 8:47 PM, Christopher Swasey <
Kernel.const_get(model)

And hence Kernel.const_get(model).new.

Arlen
 
J

John Barnette

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
AmeliaAmad

Latest Threads

Top