Solved

Find class from string

Posted on 2008-10-21
3
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is Ruby On Rails relevant 3 521
String to Hex for UDP packet 1 961
PHP vs Ruby on Rails 3 491
Help with Redmine installation on Ubuntu 14.04 6 355
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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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