Forum: Ruby on Rails How to list all models of an application?!?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
071bed059d68c032b5c07129192ab52c?d=identicon&s=25 Edoardo Marcora (marcora)
on 2006-05-23 22:20
(Received via mailing list)
How can I get a list of all model classes in the domain of a Rails
application (all models, both in "app/models" and in
components/somedir/model.rb)?

Thanx in advance for your precious help!

Edoardo "Dado" Marcora
Ff9ba6ea540d0d42d520a33107c3ac31?d=identicon&s=25 Erik B. Ordway (Guest)
on 2006-05-24 01:51
(Received via mailing list)
Here is a god awful hack of some of the code from UserEngine to get
controllers.  All uglyness is mine and should not reflect poorly on
UserEngine.  UserEngine is an innocent bystander in this case.
Changing the path and the split stuff might make it work for you.


     # Load all the controller files
     controller_files = Dir[RAILS_ROOT + "/app/controllers/**/
*_controller.rb"]

    # should we check to see if this is defined? I.E. will this code
ever run
       # outside of the framework environment...?
       controller_files += Dir[Engines.config(:root) + "/**/app/
controllers/**/*_controller.rb"]

     # we need to get all the controller names
     @controllers = Hash.new
     controller_files.each do |file_name|
       file_name = file_name.split("\/").last.chomp("_controller.rb")
       @controllers[file_name.pluralize] = file_name.titleize
       #require file_name #if /_controller.rb$/ =~ file_name
     end

This gets me a hash that I use to build a menu for an app.  I also
use the following to clear certain types out off the hash.
   def clear_admin(controllers)
     controllers.delete_if {|key,value| key == "users"}
     controllers.delete_if {|key,value| key == "logins"}
   end

   def clear_lookups(controllers)
     controllers.delete_if {|key,value| key == "method_of_estimations"}
     controllers.delete_if {|key,value| key == "method_of_observations"}
     controllers.delete_if {|key,value| key == "nests_types"}
     controllers.delete_if {|key,value| key == "decay_classes"}
     controllers.delete_if {|key,value| key == "food_types"}
     controllers.delete_if {|key,value| key == "feed_methods"}
     controllers.delete_if {|key,value| key == "species"}

   end
071bed059d68c032b5c07129192ab52c?d=identicon&s=25 Edoardo Marcora (marcora)
on 2006-05-24 03:20
(Received via mailing list)
Thanx Erik... that helps a lot and it's probably the way I will go by!!!

However, I was wondering whether there is a method that could retrieve
the
model list somewhere in the "glue" layer that ties together ActionPack
and
ActiveRecord in Rails.

Thanx again,

Dado
13a61ecd904e57976e2bfafd86219567?d=identicon&s=25 j`ey (Guest)
on 2006-05-24 08:35
Edoardo Marcora wrote:
> Thanx Erik... that helps a lot and it's probably the way I will go by!!!
>
> However, I was wondering whether there is a method that could retrieve
> the
> model list somewhere in the "glue" layer that ties together ActionPack
> and
> ActiveRecord in Rails.
>
> Thanx again,
>
> Dado
ActiveRecord::Base.subclasses

j`ey
http://www.eachmapinject.com
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-24 08:47
(Received via mailing list)
ActiveRecord::Bases.subclasses is protected.

Object.subclasses_of(ActiveRecord::Base) should do what you want, but
you will need to ensure that all the model files have been loaded.
This should do it:

Dir.glob(RAILS_ROOT + '/app/models/**/*.rb').each { |file| require file
}

-Jonathan.
13a61ecd904e57976e2bfafd86219567?d=identicon&s=25 j`ey (Guest)
on 2006-05-24 10:22
Jonathan Viney wrote:
> ActiveRecord::Bases.subclasses is protected.

ActiveRecord::Base.send(:subclasses)

j`ey
http://www.eachmapinject.com
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Guest (Guest)
on 2006-05-24 14:46
Also this: http://rav.rubyforge.org/
Gives you a image of your models...
This topic is locked and can not be replied to.