# Looping through all models in the application

**URL:** <https://www.ruby-forum.com/t/looping-through-all-models-in-the-application/90058>\
**Category:** Rails\
**Created:** [January 5, 2006, 11:35am UTC](https://www.ruby-forum.com/t/looping-through-all-models-in-the-application/90058 "2006-01-05T11:35:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henning\_KSPedersen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Henning\_KSPedersen](https://www.ruby-forum.com/u/Henning_KSPedersen)\
**Post date:** [January 5, 2006, 11:35am UTC](https://www.ruby-forum.com/t/looping-through-all-models-in-the-application/90058/1 "2006-01-05T11:35:49Z")

</div>

Hello.

I have an application that depends on a table/model  
(ModelInternationalization) that provides international translations in  
various languages for all the model/table and columns names as well as  
some other stuff (whether the column is visible on forms, dropdowns,  
lists, reports etc).

I need to create a management interface for this translation stuff (the  
plaintext stuff, btw, will be handled by GetText). To do this, I need to  
loop through all the models in the application and check if they are  
present for the currently selected language in the translation model.  
Maybe some pseudocode would help clarify

for m in Application models  
\<%= h m.model.name %\>  
end

Essentially, this would loop through all the models available to the  
application and just display it’s name. Ofcourse, the actually code is  
much more complex.

Is there a collection of the available models somewhere? Or do I need to  
goof it up and just parse the directory listing of the app/models  
directory? Or worse, create a table with just the names of the models  
and loop through that?

Regards,  
Henning P.

---

<div class="post-metadata">

**Author:** ![Henning\_KSPedersen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/73ab20/32.png) [@Henning\_KSPedersen](https://www.ruby-forum.com/u/Henning_KSPedersen)\
**Post date:** [January 7, 2006, 1:55am UTC](https://www.ruby-forum.com/t/looping-through-all-models-in-the-application/90058/2 "2006-01-07T01:55:47Z")

</div>

On 1/5/06, Henning K. Pedersen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> loop through all the models in the application and check if they are
> 
> Is there a collection of the available models somewhere? Or do I need to  
> goof it up and just parse the directory listing of the app/models  
> directory? Or worse, create a table with just the names of the models  
> and loop through that?

This is really ugly, but this will give you a list of all objects that  
subclass from ActiveRecord::Base (which are all your models):

ObjectSpace.each\_object do |object|  
if object.class == Class and object.superclass == ActiveRecord::Base  
puts object.to\_s  
end  
end

You’ll probably need to skip CGI::Session::ActiveRecordStore::Session  
if you use ActiveRecordStore for your sessions.

- Jamie
