开发者

Calling new on the db string name

开发者 https://www.devze.com 2023-01-14 07:48 出处:网络
Ok so i have the string \"Product\". table_name = \"Product\" I cant do table_name.new undefined method `new

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

精彩评论

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