2015-04-22 40 views
0

我有一些模型需要动态初始化。如何使用动态创建的模型调用模型方法

model = <INPUT_STRING>.capitalize.constantize.new 

但我不能叫where方法,通过model.where(~)

它会显示

NoMethodError: undefined method `where' for #<User:0x007fb1142a0978> 
+1

您试图在'User'实例上调用'where'而不是class。顺便说一句,你是否以某种方式清理你的输入? –

回答

0

所有你需要不要创建新的对象

model = <INPUT_STRING>.capitalize.constantize 

这就是

0

在您的代码中,modelUser类的一个实例。删除代码行末尾的new方法以使其起作用。