Where to get list of all ActiveRecord classes

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

On May 30, 9:52 pm, Pablo C. [email protected]
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

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

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. [email protected]
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

Pablo,

Dir.glob("#{RAILS_ROOT}/app/models/**/*rb").each{|m|
Dependencies.require_or_load m }
Object.subclasses_of ActiveRecord::Base

  • Lourens