views:

28

answers:

2

Ok so i have the string "Product".

table_name = "Product"

I cant do

table_name.new

undefined method `new

So i was trying to find 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

A: 

try this:

(instance_eval table_name).new
ennuikiller
+2  A: 

If you want to avoid calling eval you can also do

"product".camelize.constantize.new

jdl