Hi, I''m trying to get a list of all models in my app without looking at the /models folder on disk (long story). I thought I''d just get the table names and convert them into class names: ActiveRecord::Base.connection.tables.map { |t| t.classify } But there are a couple of exceptions, like schema_info and schema_migrations. So I thought I''d check to see if the resulting classifcation actually exists as a class, but I can only think of an ugly way to check: eval t.classify rescue nil # throws an exception if the class does not exist Is there any way I can avoid the ugly eval? Thanks! Jeff --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I remember vaguely that ActiveRecord keeps track of all the models derived from it. It had some protected method to get them all which you could access with the send method. Have a look in the ActiveRecord sources to get more details. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Another way to do it is that there is a ruby method called const_defined? or something like that, you can google for it, and you can check to see if the class name is defined. Jamey On Fri, Jul 11, 2008 at 12:25 PM, Thorsten Müller <thorsten-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> > I remember vaguely that ActiveRecord keeps track of > all the models derived from it. > It had some protected method to get them all which you > could access with the send method. > > Have a look in the ActiveRecord sources to get more details. > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ActiveRecord::Base.send(:subclasses) And then you probably want to avoid some (like CGI::Session::ActiveRecordStore::Session) I use this in: def self.posers [CGI::Session::ActiveRecordStore::Session] end for klass in ActiveRecord::Base.send(:subclasses) next if posers.include?(klass) Hope that helps. -Rob On Jul 11, 2008, at 12:22 PM, Jeff wrote:> > Hi, > > I''m trying to get a list of all models in my app without looking at > the /models folder on disk (long story). > > I thought I''d just get the table names and convert them into class > names: > > ActiveRecord::Base.connection.tables.map { |t| t.classify } > > But there are a couple of exceptions, like schema_info and > schema_migrations. So I thought I''d check to see if the resulting > classifcation actually exists as a class, but I can only think of an > ugly way to check: > > eval t.classify rescue nil # throws an exception if the class does > not exist > > Is there any way I can avoid the ugly eval? > > Thanks! > Jeff > > >Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org +1 513-295-4739 Skype: rob.biedenharn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---