Solved

Find class from string

Posted on 2008-10-21
3
307 Views
Last Modified: 2013-11-13
I have many classes (models) called (something)Configuration.

I have a variable named "whatToDo"

Where whatToDo = "sendConfiguration"

What I want to do is    whatToDo.find_by_sql(sql)

It works if I write it sendConfiguration.find_by_sql(sql) but not whatToDo.find_by_sql(sql) where whatToDo = "sendConfiguration". Is there a method that finds a class from a string?
0
Comment
Question by:maxbillys
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Andrew Doades
ID: 22768080
you would need to do WhatToDo.find_by_sql(sql) with a capital W at the start?

This would define the model.
0
 
LVL 14

Accepted Solution

by:
wesgarrison earned 30 total points
ID: 22768253
See const_get()
http://www.ruby-doc.org/core/classes/Module.html#M001712
whatToDo = "sendConfiguration"  # class name

class_object = Object.const_get(whatToDo)

class_object.find_by_sql(...)

Open in new window

0
 
LVL 10

Assisted Solution

by:Andrew Doades
Andrew Doades earned 20 total points
ID: 22768622
Sorry, I thought you was trying to find something from the database.

I just added some code into my project and got a result, see attached.

I changed my database and model to  sendConfiguration, to try and help
action = Object.const_get(" sendConfiguration")

action.find_by_sql()

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now