Hello, I have an interessting problem, I''m not sure how to solve :-) I have the name of a model in a variable like this: My model is called Article My var contains "Article" Now - How do I get the model class, so I can call Article.find, Article.new etc ... ? Greetings, Gitte Wange
On 1/6/06, Gitte Wange <gitte@wange.dk> wrote:> Hello, > > I have an interessting problem, I''m not sure how to solve :-) > I have the name of a model in a variable like this: > My model is called Article > My var contains "Article" > > Now - How do I get the model class, so I can call Article.find, > Article.new etc ... ? > > Greetings, > Gitte Wange''Article''.constantize -- rick http://techno-weenie.net
Gitte Wange wrote:> I have an interessting problem, I''m not sure how to solve :-) > I have the name of a model in a variable like this: > My model is called Article > My var contains "Article" > > Now - How do I get the model class, so I can call Article.find, > Article.new etc ... ?eval("Article").find and eval("Article").new. I believe there''s at least one other way, but it eludes me at the moment. Do realize though, that this poses a big security hole if you pass tainted data to eval. eval("Article.destroy_all && Article") is fairly boring to run for example. -- Jakob Skjerning - http://mentalized.net
Jakob L. Skjerning wrote:> Gitte Wange wrote: > >> I have an interessting problem, I''m not sure how to solve :-) >> I have the name of a model in a variable like this: >> My model is called Article >> My var contains "Article" >> >> Now - How do I get the model class, so I can call Article.find, >> Article.new etc ... ? > > > eval("Article").find and eval("Article").new. I believe there''s at least > one other way, but it eludes me at the moment. > > Do realize though, that this poses a big security hole if you pass > tainted data to eval. eval("Article.destroy_all && Article") is fairly > boring to run for example. >I''m aware of the security issues. But I won''t call .destroy or anything - and the classes are taking from a db and evaluated before. It''s just for some lookup methods in my model. Thank you very much for the help. Greetings, Gitte Wange