rails eval code

Posted by xpepermint on Stack Overflow See other posts from Stack Overflow or by xpepermint
Published on 2010-03-28T18:01:17Z Indexed on 2010/03/28 18:03 UTC
Read the original article Hit count: 154

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about eval