Find class from string

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?
Who is Participating?
wesgarrisonConnect With a Mentor Commented:
See const_get()
whatToDo = "sendConfiguration"  # class name
class_object = Object.const_get(whatToDo)

Open in new window

Andrew DoadesIT TechnicianCommented:
you would need to do WhatToDo.find_by_sql(sql) with a capital W at the start?

This would define the model.
Andrew DoadesConnect With a Mentor IT TechnicianCommented:
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")

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.