# Finding out what models exist in your application

**URL:** <https://www.ruby-forum.com/t/finding-out-what-models-exist-in-your-application/140601>\
**Category:** Rails\
**Created:** [June 16, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/finding-out-what-models-exist-in-your-application/140601 "2008-06-16T18:35:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![saljamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@saljamil](https://www.ruby-forum.com/u/saljamil)\
**Post date:** [June 16, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/finding-out-what-models-exist-in-your-application/140601/1 "2008-06-16T18:35:48Z")

</div>

Is there a way to reflect in Rails and find out what models are  
defined in your application? Maybe generate an Array with model names?  
Thanks

---

<div class="post-metadata">

**Author:** ![saljamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@saljamil](https://www.ruby-forum.com/u/saljamil)\
**Post date:** [June 16, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/finding-out-what-models-exist-in-your-application/140601/2 "2008-06-16T18:47:02Z")

</div>

On Jun 16, 2008, at 12:34 PM, saljamil wrote:

> Is there a way to reflect in Rails and find out what models are  
> defined in your application? Maybe generate an Array with model names?  
> Thanks

Well, you can look at the files in app/models/, but I suspect that’s  
not what you want to hear.

If you mean specifically the models that are based on ActiveRecord,  
then you can do:

ActiveRecord::Base.send(:subclasses)

It’s peeking under the covers a bit because it is a private method  
(hence the use of send). If you have STI models, only the top-most  
one is a direct subclass of ActiveRecord::Base so you have to deal  
with that if it is any issue for you.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![saljamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@saljamil](https://www.ruby-forum.com/u/saljamil)\
**Post date:** [June 16, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/finding-out-what-models-exist-in-your-application/140601/3 "2008-06-16T18:48:13Z")

</div>

On 16 Jun 2008, at 17:46, Rob B. wrote:

> If you mean specifically the models that are based on ActiveRecord,  
> then you can do:
> 
> ActiveRecord::Base.send(:subclasses)

There is however the issue that this won’t find subclasses that  
haven’t been loaded yet.

Fred

---

<div class="post-metadata">

**Author:** ![saljamil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@saljamil](https://www.ruby-forum.com/u/saljamil)\
**Post date:** [June 16, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/finding-out-what-models-exist-in-your-application/140601/4 "2008-06-16T19:05:52Z")

</div>

On Jun 16, 2008, at 12:47 PM, Frederick C. wrote:

> > If you mean specifically the models that are based on ActiveRecord,  
> > then you can do:
> > 
> > ActiveRecord::Base.send(:subclasses)
> 
> There is however the issue that this won’t find subclasses that  
> haven’t been loaded yet.
> 
> Fred

Well, yes there is that. 😉 In the code I have that uses this,  
there’s first:

```
 for f in Dir.glob(File.join(RAILS_ROOT || '.', "app/models",

```

“\*.rb”))  
puts “getting #{f}…” if options.debug  
require f  
end

So I avoid that particular problem.

Perhaps if the OP tells us what is really needed…

ActiveRecord::Base.connection.tables.reject{|t|t=~/schema/}.map do |  
t|  
t.singularize.camelize.constantize  
end

-Rob
