# List of all Models

**URL:** <https://www.ruby-forum.com/t/list-of-all-models/57215>\
**Category:** Rails\
**Created:** [April 7, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/list-of-all-models/57215 "2006-04-07T23:38:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 7, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/list-of-all-models/57215/1 "2006-04-07T23:38:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 8, 2006, 12:48am UTC](https://www.ruby-forum.com/t/list-of-all-models/57215/2 "2006-04-08T00:48:33Z")

</div>

On 4/7/06, Hampton [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 8, 2006, 3:04am UTC](https://www.ruby-forum.com/t/list-of-all-models/57215/3 "2006-04-08T03:04:40Z")

</div>

> 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 B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote  
in message  
news:removed\_email\_address@domain.invalid…  
On 4/7/06, Hampton [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![Hampton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Hampton](https://www.ruby-forum.com/u/Hampton)\
**Post date:** [April 8, 2006, 4:41am UTC](https://www.ruby-forum.com/t/list-of-all-models/57215/4 "2006-04-08T04:41:44Z")

</div>

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.
