Hi, I would like to create an active record object from a string that I will be passed. I could use eval but the documentation says that this can be slow. Is there any other simple way to create an record object at run time. Thank, HJS -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What''s in the string ? Fred -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> What''s in the string ?Sorry, I mean create an active record object from a string containing the name of the object (which I won''t know till runtime). HJS -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
H. joseph Solbrig wrote:> Hi, > > I would like to create an active record object from a string that I will > be passed. I could use eval but the documentation says that this can be > slow. Is there any other simple way to create an record object at run > time. > > Thank, > > HJSHi, Generally the way to do it would be something like this: myStr = "Post" posts = myStr.constantize.find(:all) Essentially what that is doing is taking the string "Post", converting it to the class with the name Post. So once you call constantize on that string, you can call any of Post''s methods. In the end of this code, the variable posts would contain a collection of all Post ActiveRecord objects. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Watch out! This can be a major security hole, if you''re not 100% sure of whats in my_str. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marc Baumbach wrote:> H. joseph Solbrig wrote: >> Hi, >> >> I would like to create an active record object from a string...> > myStr = "Post" > posts = myStr.constantize.find(:all) >...... How would you find out if the class "Post" existed? (This is probably a newby but haven''t found it in my searches..) HJS -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
try defined?(myStr.constantized) == "constant" If it is defined, it will spit back "constant", otherwise you could see "method". Normally, if you called defined? on an actual constant that isnt there (i.e. defined?(Poster) ) you would get back nil, but since you are trying to check the output of a method, instead of returning nil it returns "method" hope that helps --jake H. joseph Solbrig wrote:> Marc Baumbach wrote: >> H. joseph Solbrig wrote: >>> Hi, >>> >>> I would like to create an active record object from a string > ... >> >> myStr = "Post" >> posts = myStr.constantize.find(:all) >> > ...... > > How would you find out if the class "Post" existed? > > (This is probably a newby but haven''t found it in my searches..) > > HJS-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just out of curiosity, do you know why constantize uses module_eval and not const_get? If I was doing it without constantize, I would do it as: begin obj = Kernel.const_get(my_str).new rescue puts "no class named #{my_str}" end Michael> Hi, > > Generally the way to do it would be something like this: > > myStr = "Post" > posts = myStr.constantize.find(:all) > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---