[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Typecast

Sijo Kg

3/3/2008 9:39:00 AM

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
--
Posted via http://www.ruby-....

3 Answers

Christopher Swasey

3/3/2008 9:47:00 AM

0

On 3/3/08, Sijo Kg <sijo@maxxion.com> wrote:
> 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

Arlen Cuss

3/3/2008 11:48:00 AM

0

[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 <
christopher.swasey@gmail.com> wrote:

> Kernel.const_get(model)
>

And hence Kernel.const_get(model).new.

Arlen

John Barnette

3/3/2008 10:33:00 PM

0

On Mon, Mar 3, 2008 at 1:39 AM, Sijo Kg <sijo@maxxion.com> 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.