Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find class from string

Posted on 2008-10-21
3
Medium Priority
?
316 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 120 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 80 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month6 days, 22 hours left to enroll

782 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