Anyone know a pretty way to get a list of all Models? That is, a list of all classes which inherit from ActiveRecord:Base I can''t seem to figure it out! The best I''ve got is to list the /app/models directory... but, that is *dirty*. -hampton. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060407/b9be5eda/attachment-0001.html
On 4/7/06, Hampton <hcatlin@gmail.com> wrote:> Anyone know a pretty way to get a list of all Models? > > That is, a list of all classes which inherit from ActiveRecord:Base > > I can''t seem to figure it out! > > The best I''ve got is to list the /app/models directory... but, that is > *dirty*.You can do this, but it will only show all of your models once they have been loaded. If nothing has ''require''d them yet, they won''t show up. found = [] ObjectSpace.each_object(Class) do |klass| found << klass if klass.ancestors.include?(ActiveRecord::Base) end [ActiveRecord::Base, CGI::Session::ActiveRecordStore::Session, SomeModel, SomeOtherModel] A shortcut (which I don''t recommend) is to call: ActiveRecord::Base.send(:subclasses) ..which is probably a bad idea, because it invokes a private method on ActiveRecord::Base.
> Anyone know a pretty way to get a list of all Models?Not tested but something like the code below ... def list_all_models ObjectSpace.each_object(Class) do |klass| return klass.select {|k| k.kind_of?(ActiveRecord::Base)} end end Steven Beales "Wilson Bilkovich" <wilsonb@gmail.com> wrote in message news:d4e4955b0604071545y7083320i6efe8b9fb00f1f1a@mail.gmail.com... On 4/7/06, Hampton <hcatlin@gmail.com> wrote:> Anyone know a pretty way to get a list of all Models? > > That is, a list of all classes which inherit from ActiveRecord:Base > > I can''t seem to figure it out! > > The best I''ve got is to list the /app/models directory... but, that is > *dirty*.You can do this, but it will only show all of your models once they have been loaded. If nothing has ''require''d them yet, they won''t show up. found = [] ObjectSpace.each_object(Class) do |klass| found << klass if klass.ancestors.include?(ActiveRecord::Base) end [ActiveRecord::Base, CGI::Session::ActiveRecordStore::Session, SomeModel, SomeOtherModel] A shortcut (which I don''t recommend) is to call: ActiveRecord::Base.send(:subclasses) ..which is probably a bad idea, because it invokes a private method on ActiveRecord::Base.
You''ve got to make sure all the models have been loaded first. This
should
work:
Dir.glob(RAILS_ROOT + ''/app/models/*.rb'').each { |file|
require file }
models = Object.subclasses_of(ActiveRecord::Base)
-Jonathan.
On 4/8/06, Hampton <hcatlin@gmail.com> wrote:>
> Anyone know a pretty way to get a list of all Models?
>
> That is, a list of all classes which inherit from ActiveRecord:Base
>
> I can''t seem to figure it out!
>
> The best I''ve got is to list the /app/models directory... but,
that is
> *dirty*.
>
> -hampton.
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060408/39ecd111/attachment.html