Solved

Find class from string

Posted on 2008-10-21
3
306 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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

12 Experts available now in Live!

Get 1:1 Help Now