rails eval code
- by xpepermint
Hey. I have to do a coll inside my model like this:
import = Import.find(id)
status = User.find(import.user_id).{XXX}.import(import.file.path)
Notice a {XXX} which should be replaced by a dinamic variable of a submodel. Model User has_many groups, clients and products. In translation this would be
status = User.find(import.user_id).groups.import(import.file.path)
status = User.find(import.user_id).clients.import(import.file.path)
status = User.find(import.user_id).products.import(import.file.path)
I was thinking of
import = Import.find(id)
status = eval("User.find(#{import.user_id}).#{import.model}").import(import.file.path)
but this gives me an error 'TypeError: can't convert nil into String'.
Please tell me how would you fix that. Thx!