开发者

Calling new on the db string name

Ok so i have the string "Product".

table_name = "Product"

I cant do

table_name.new

undefined method `new

So i was trying to f开发者_运维问答ind a work around like this

table_name = table_name.downcase.pluralize
name = ActiveRecord::Base.connection.tables.select { |t| t == table_name }.first
name.new

I am not sure this will work but even if it does it looks like a hack, any ideas to clean this up


If you want to avoid calling eval you can also do

"product".camelize.constantize.new


try this:

(instance_eval table_name).new
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜