Hi everyone, I''m writing a piece of software where I receive the name of an active record class from the outside (in the request_uri) and want to make a lookup on whether this active record class exists. Then, I need to do a .find(...) and whatever on that class. The following lines do exactly what I want, however, they are completely *NOT* secure :-) def get_activerecord_named(name) activerecord = eval(name) if activerecord.superclass == ActiveRecord::Base return activerecord else return nil end end Anyone has a better idea? A switch/case is not an option as I don''t know the names of all existing active record classes at design time. However, could one find out the names of all available active record classes at run time? Thank you, your help is very appreciated! --jan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Populate a list of models at server startup? On 27/10/06, yeah <gnuemac-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hi everyone, > > I''m writing a piece of software where I receive the name of an active > record class from the outside (in the request_uri) and want to make a > lookup on whether this active record class exists. Then, I need to do a > .find(...) and whatever on that class. > > The following lines do exactly what I want, however, they are > completely *NOT* secure :-) > > def get_activerecord_named(name) > activerecord = eval(name) > if activerecord.superclass == ActiveRecord::Base > return activerecord > else > return nil > end > end > > Anyone has a better idea? > > A switch/case is not an option as I don''t know the names of all > existing active record classes at design time. However, could one find > out the names of all available active record classes at run time? > > Thank you, your help is very appreciated! > > --jan > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
how would you do that? On Oct 27, 1:51 pm, "Ian Leitch" <port...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Populate a list of models at server startup? > > On 27/10/06, yeah <gnue...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi everyone, > > > I''m writing a piece of software where I receive the name of an active > > record class from the outside (in the request_uri) and want to make a > > lookup on whether this active record class exists. Then, I need to do a > > .find(...) and whatever on that class. > > > The following lines do exactly what I want, however, they are > > completely *NOT* secure :-) > > > def get_activerecord_named(name) > > activerecord = eval(name) > > if activerecord.superclass == ActiveRecord::Base > > return activerecord > > else > > return nil > > end > > end > > > Anyone has a better idea? > > > A switch/case is not an option as I don''t know the names of all > > existing active record classes at design time. However, could one find > > out the names of all available active record classes at run time? > > > Thank you, your help is very appreciated! > > > --jan--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
yeah wrote:> Hi everyone, > > I''m writing a piece of software where I receive the name of an active > record class from the outside (in the request_uri) and want to make a > lookup on whether this active record class exists. Then, I need to do a > .find(...) and whatever on that class. > > The following lines do exactly what I want, however, they are > completely *NOT* secure :-) > > def get_activerecord_named(name) > activerecord = eval(name) > if activerecord.superclass == ActiveRecord::Base > return activerecord > else > return nil > end > end > > Anyone has a better idea? > > A switch/case is not an option as I don''t know the names of all > existing active record classes at design time. However, could one find > out the names of all available active record classes at run time?ActiveRecord::Base only finds out about its subclasses when they are loaded. I think it''s easier to follow your approach, finding the named class (if there is one), and then finding out if its superclass is ActiveRecord::Base. (Note that if you start introducing inheritance among your model classes you will need to extend this to look beyond the direct superclass.) Try this: def get_activerecord_named(name) c = name.constantize rescue nil if c.class == Class && c.superclass == ActiveRecord::Base return c else return nil end end Rails adds the constantize method to String, to return the value of the constant named by the String. If there isn''t one, it raises a NameError exception - the "rescue nil" clause catches this. In the if statement, c could be nil, or some non-class constant value, or a class - so a check that it''s a class is needed before trying to invoke its superclass method. regards Justin Forder --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you, Justin. That looks great! Justin Forder wrote:> yeah wrote: > > Hi everyone, > > > > I''m writing a piece of software where I receive the name of an active > > record class from the outside (in the request_uri) and want to make a > > lookup on whether this active record class exists. Then, I need to do a > > .find(...) and whatever on that class. > > > > The following lines do exactly what I want, however, they are > > completely *NOT* secure :-) > > > > def get_activerecord_named(name) > > activerecord = eval(name) > > if activerecord.superclass == ActiveRecord::Base > > return activerecord > > else > > return nil > > end > > end > > > > Anyone has a better idea? > > > > A switch/case is not an option as I don''t know the names of all > > existing active record classes at design time. However, could one find > > out the names of all available active record classes at run time? > > ActiveRecord::Base only finds out about its subclasses when they are > loaded. I think it''s easier to follow your approach, finding the named > class (if there is one), and then finding out if its superclass is > ActiveRecord::Base. (Note that if you start introducing inheritance > among your model classes you will need to extend this to look beyond the > direct superclass.) > > Try this: > > def get_activerecord_named(name) > c = name.constantize rescue nil > if c.class == Class && c.superclass == ActiveRecord::Base > return c > else > return nil > end > end > > Rails adds the constantize method to String, to return the value of the > constant named by the String. If there isn''t one, it raises a NameError > exception - the "rescue nil" clause catches this. > > In the if statement, c could be nil, or some non-class constant value, > or a class - so a check that it''s a class is needed before trying to > invoke its superclass method. > > regards > > Justin Forder--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Where are the ruby logs on Mac OS X Tiger? I need to see the SQL commands the update_all method of ActiveRecord is sending. Manually running the output of class MyTable < ActiveRecord: end puts "UPDATE my_table SET #{updates} WHERE #{conditions} MyTABLE.update_all(updates, conditions) yeilds very different results from the result of the command. scope is not set, so should not be a factor. Seeing what ActiveRecord is saying to MySQL would really help. Thanks, Ric Turley --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Look at your log/development.log. Also the query_trace plugin will be useful. Vish On 11/6/06, Ric Turley <lists-M4cQsOMrfNkAvxtiuMwx3w@public.gmane.org> wrote:> > Where are the ruby logs on Mac OS X Tiger? > > I need to see the SQL commands the update_all method of ActiveRecord is > sending. Manually running the output of > > class MyTable < ActiveRecord: end > puts "UPDATE my_table SET #{updates} WHERE #{conditions} > MyTABLE.update_all(updates, conditions) > > yeilds very different results from the result of the command. scope is not > set, so should not be a factor. > > Seeing what ActiveRecord is saying to MySQL would really help. > > Thanks, > Ric Turley > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the reply. Actually, I am doing the "Use ActiveRecord Outside of Rails" trick as I haven''t figured out how to do the user interface yet. I am writing a method to move a sub-tree in an acts_as_nested_set module. Hence, I am trying to find the Ruby logs rather than the Rails logs. Thanks again, Ric Turley> From: "Vishnu Gopal" <g.vishnu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Reply-To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Date: Tue, 7 Nov 2006 00:10:09 +0530 > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: ActiveRecord logging... > > > Look at your log/development.log. Also the query_trace plugin will be useful. > > Vish > > On 11/6/06, Ric Turley <lists-M4cQsOMrfNkAvxtiuMwx3w@public.gmane.org> wrote: >> >> Where are the ruby logs on Mac OS X Tiger? >> >> I need to see the SQL commands the update_all method of ActiveRecord is >> sending. Manually running the output of >> >> class MyTable < ActiveRecord: end >> puts "UPDATE my_table SET #{updates} WHERE #{conditions} >> MyTABLE.update_all(updates, conditions) >> >> yeilds very different results from the result of the command. scope is not >> set, so should not be a factor. >> >> Seeing what ActiveRecord is saying to MySQL would really help. >> >> Thanks, >> Ric Turley >> >> >>> >> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---