# Where to get list of all ActiveRecord classes

**URL:** <https://www.ruby-forum.com/t/where-to-get-list-of-all-activerecord-classes/105565>\
**Category:** Rails\
**Created:** [May 31, 2007, 5:52am UTC](https://www.ruby-forum.com/t/where-to-get-list-of-all-activerecord-classes/105565 "2007-05-31T05:52:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@pcollins](https://www.ruby-forum.com/u/pcollins)\
**Post date:** [May 31, 2007, 5:52am UTC](https://www.ruby-forum.com/t/where-to-get-list-of-all-activerecord-classes/105565/1 "2007-05-31T05:52:35Z")

</div>

Anybody know how to get a list of all your model classes from a rails  
environment?

I’d like to do something like “ActiveRecord::Base.subclasses” but that  
gives me:

pcollins@horatio:~/proj/foobox$ script/console  
Loading development environment.

> > ActiveRecord::Base.subclasses  
> > NoMethodError: protected method `subclasses’ called …

Anybody know how to do this? (getting a directory listing doesn’t count)

Much obliged,

PMC

---

<div class="post-metadata">

**Author:** ![pcollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@pcollins](https://www.ruby-forum.com/u/pcollins)\
**Post date:** [May 31, 2007, 6:28am UTC](https://www.ruby-forum.com/t/where-to-get-list-of-all-activerecord-classes/105565/2 "2007-05-31T06:28:14Z")

</div>

On May 30, 9:52 pm, Pablo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’d like to do something like “ActiveRecord::Base.subclasses” but that  
> gives me:
> 
> pcollins@horatio:~/proj/foobox$ script/console  
> Loading development environment.
> 
> > > ActiveRecord::Base.subclasses  
> > > NoMethodError: protected method `subclasses’ called …

You can bypass protected/private restrictions by using send.

ActiveRecord::Base.send(:subclasses)

That will only give you classes that have been loaded already.

> Anybody know how to do this? (getting a directory listing doesn’t count)

It may be cheating but that’s how I’ve used the directory listing  
before:

Dir.glob(“app/models/\*rb”)

Aaron

---

<div class="post-metadata">

**Author:** ![pcollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@pcollins](https://www.ruby-forum.com/u/pcollins)\
**Post date:** [June 2, 2007, 3:58am UTC](https://www.ruby-forum.com/t/where-to-get-list-of-all-activerecord-classes/105565/3 "2007-06-02T03:58:49Z")

</div>

I was kind of hoping for a public-method solution. ☹

I suppose I’ll just get the directory listing as you mention in that  
case.

Thank you for your help friend.

-Pablo

Aaron wrote:

> On May 30, 9:52 pm, Pablo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I’d like to do something like “ActiveRecord::Base.subclasses” but that  
> > gives me:
> > 
> > pcollins@horatio:~/proj/foobox$ script/console  
> > Loading development environment.
> > 
> > > > ActiveRecord::Base.subclasses  
> > > > NoMethodError: protected method `subclasses’ called …
> 
> You can bypass protected/private restrictions by using send.
> 
> ActiveRecord::Base.send(:subclasses)
> 
> That will only give you classes that have been loaded already.
> 
> > Anybody know how to do this? (getting a directory listing doesn’t count)
> 
> It may be cheating but that’s how I’ve used the directory listing  
> before:
> 
> Dir.glob(“app/models/\*rb”)
> 
> Aaron

---

<div class="post-metadata">

**Author:** ![pcollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@pcollins](https://www.ruby-forum.com/u/pcollins)\
**Post date:** [June 2, 2007, 4:15am UTC](https://www.ruby-forum.com/t/where-to-get-list-of-all-activerecord-classes/105565/4 "2007-06-02T04:15:42Z")

</div>

Pablo,

Dir.glob("#{RAILS\_ROOT}/app/models/\*\*/\*rb").each{|m|  
Dependencies.require\_or\_load m }  
Object.subclasses\_of ActiveRecord::Base

- Lourens
