Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Class method dynamically in ruby

Say I have the model name saved in a variable:

"#{class_name.singularize}"

from another controller I want to see the columns defined for this model. I tried

send("#{class_name.singularize}.columns")

but its trying to call Page.columns as a method of the class I am currently working in rather than the Page class. Any ideas on how to do this?

like image 245
j_mcnally Avatar asked Mar 11 '12 06:03

j_mcnally


1 Answers

Use constantize:

class_name.singularize.constantize.columns
like image 86
Andrew Marshall Avatar answered Sep 18 '22 16:09

Andrew Marshall