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.
Edoardo M. (Guest)
on 2006-05-24 00: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
Erik B. Ordway (Guest)
on 2006-05-24 03: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
Edoardo M. (Guest)
on 2006-05-24 05: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
j`ey (Guest)
on 2006-05-24 10: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
Jonathan V. (Guest)
on 2006-05-24 10: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.
j`ey (Guest)
on 2006-05-24 12:22
Jonathan V. wrote:
> ActiveRecord::Bases.subclasses is protected.

ActiveRecord::Base.send(:subclasses)

j`ey
http://www.eachmapinject.com
Guest (Guest)
on 2006-05-24 16:46
Also this: http://rav.rubyforge.org/
Gives you a image of your models...
This topic is locked and can not be replied to.