How can we instantiate a class from its name string in Ruby-on-Rails?
For example, we have this name in the database in the format "ClassName" or "my_super_class_name".
How can we create an object from it?
Decision:
I searched for it myself, but did not find it, so here it is. Ruby-on-Rails API
name = "ClassName" instance = name.constantize.new
It may not even be formatted, we can use the custom line method .classify
name = "my_super_class" instance = name.classify.constantize.new
Of course, perhaps this is not a very βRails pathβ, but it solves its purpose.
string instantiation class ruby-on-rails ruby-on-rails-3
Vjatseslav Gedrovits Dec 28 2018-12-12T00: 00Z
source share