Given a symbol in rails, how do I get a Class? So I could call something like:
give_class(:post).find(:all)
or similar.
First, convert to string.
class_name = symbol.to_s
From there, you will need to format the string into a proper class name using the methods provided by ActiveSupport's Inflector.
camelize
will turn 'my_module'
into 'MyModule'
classify
will turn 'my_models'
into 'MyModel'
camelize
is more likely the one you want, given your code snippet.
Then use the constantize
method:
klass = class_name.constantize
Classy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With