Is there a way to reflect in Rails and find out what models are defined in your application? Maybe generate an Array with model names? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Jun-16 16:46 UTC
Re: Finding out what models exist in your application
On Jun 16, 2008, at 12:34 PM, saljamil wrote:> Is there a way to reflect in Rails and find out what models are > defined in your application? Maybe generate an Array with model names? > ThanksWell, you can look at the files in app/models/, but I suspect that''s not what you want to hear. If you mean specifically the models that are based on ActiveRecord, then you can do: ActiveRecord::Base.send(:subclasses) It''s peeking under the covers a bit because it is a private method (hence the use of send). If you have STI models, only the top-most one is a direct subclass of ActiveRecord::Base so you have to deal with that if it is any issue for you. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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
2008-Jun-16 16:47 UTC
Re: Finding out what models exist in your application
On 16 Jun 2008, at 17:46, Rob Biedenharn wrote:> > On Jun 16, 2008, at 12:34 PM, saljamil wrote: >> Is there a way to reflect in Rails and find out what models are >> defined in your application? Maybe generate an Array with model >> names? >> Thanks > > > Well, you can look at the files in app/models/, but I suspect that''s > not what you want to hear. > > If you mean specifically the models that are based on ActiveRecord, > then you can do: > > ActiveRecord::Base.send(:subclasses) >There is however the issue that this won''t find subclasses that haven''t been loaded yet. Fred> It''s peeking under the covers a bit because it is a private method > (hence the use of send). If you have STI models, only the top-most > one is a direct subclass of ActiveRecord::Base so you have to deal > with that if it is any issue for you. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Jun-16 17:05 UTC
Re: Finding out what models exist in your application
On Jun 16, 2008, at 12:47 PM, Frederick Cheung wrote:> On 16 Jun 2008, at 17:46, Rob Biedenharn wrote: >> On Jun 16, 2008, at 12:34 PM, saljamil wrote: >>> Is there a way to reflect in Rails and find out what models are >>> defined in your application? Maybe generate an Array with model >>> names? >>> Thanks >> >> Well, you can look at the files in app/models/, but I suspect that''s >> not what you want to hear. >> >> If you mean specifically the models that are based on ActiveRecord, >> then you can do: >> >> ActiveRecord::Base.send(:subclasses) >> > There is however the issue that this won''t find subclasses that > haven''t been loaded yet. > > FredWell, yes there is that. ;-) In the code I have that uses this, there''s first: for f in Dir.glob(File.join(RAILS_ROOT || ''.'', "app/models", "*.rb")) puts "getting #{f}..." if options.debug require f end So I avoid that particular problem. Perhaps if the OP tells us what is really needed... ActiveRecord::Base.connection.tables.reject{|t|t=~/schema/}.map do | t| t.singularize.camelize.constantize end -Rob>> It''s peeking under the covers a bit because it is a private method >> (hence the use of send). If you have STI models, only the top-most >> one is a direct subclass of ActiveRecord::Base so you have to deal >> with that if it is any issue for you. >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---