In my Rails 3.1 application (with Ruby 1.9), I have a Deployer1 class , which is in the working subdirectory below the model directory
I am trying to dynamically load / create this class using this code:
clazz = item.deployer_class
If I do not use namespaces, for example something global, for example:
class Deployer1 end
Then it works fine (deployer_class = "Deployer1") - it can load the class and create the object.
If I try to put it in a module to name a little space, like this:
module Worker class Deployer1 end end
It does not work (deployer_class = "Worker :: Deployer1") - it gives an error about a missing constant, which, in my opinion, means that it cannot find the class.
Usually I can access the class in my Rails code in a static way (Worker :: Deployer1.new) - so Rails is configured correctly to load this, maybe I am loading it incorrectly ...
EDIT: So, according to Vladβs answer, the solution I chose is:
deployer_class.constantize.new
Thank you, Chris
source share