j_mcnally
j_mcnally

Reputation: 6958

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?

Upvotes: 8

Views: 3546

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96914

Use constantize:

class_name.singularize.constantize.columns

Upvotes: 14

Related Questions