# Engines question

**URL:** <https://www.ruby-forum.com/t/engines-question/100543>\
**Category:** Rails\
**Created:** [July 30, 2006, 1:59am UTC](https://www.ruby-forum.com/t/engines-question/100543 "2006-07-30T01:59:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hasan\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5e9695/32.png) [@Hasan\_D](https://www.ruby-forum.com/u/Hasan_D)\
**Post date:** [July 30, 2006, 1:59am UTC](https://www.ruby-forum.com/t/engines-question/100543/1 "2006-07-30T01:59:55Z")

</div>

I would like to get at all models in a rails application to be able to  
add a field to every one of them as part of a migration. Something  
like the following:

Appmodels.each { |x| add\_column x.table\_name.to\_s, :foo, :text }

I understand I can get the table name using  
ActiveRecord::Base#table\_name, but how do I get all the models into an  
array that I can use in the above manner?

---

<div class="post-metadata">

**Author:** ![Hasan\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5e9695/32.png) [@Hasan\_D](https://www.ruby-forum.com/u/Hasan_D)\
**Post date:** [July 30, 2006, 4:13am UTC](https://www.ruby-forum.com/t/engines-question/100543/2 "2006-07-30T04:13:41Z")

</div>

Best way I have seen so far is to do a file scan and load them directly.  
Rails loads the file in when you call the model name, so there is no  
array  
of all the models prior to calling and loading each.

-NSHB

On 7/28/06, Hasan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Cheers,  
> Hasan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Kind regards,

Nathaniel B.  
President & CEO  
Inimit Innovations Inc. - [http://inimit.com](http://inimit.com)

---

<div class="post-metadata">

**Author:** ![Hasan\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5e9695/32.png) [@Hasan\_D](https://www.ruby-forum.com/u/Hasan_D)\
**Post date:** [August 1, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/engines-question/100543/3 "2006-08-01T16:35:07Z")

</div>

On 7/29/06, Hasan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like to get at all models in a rails application to be able to  
> add a field to every one of them as part of a migration. Something  
> like the following:
> 
> Appmodels.each { |x| add\_column x.table\_name.to\_s, :foo, :text }
> 
> I understand I can get the table name using  
> ActiveRecord::Base#table\_name, but how do I get all the models into an  
> array that I can use in the above manner?

Not sure if this gives you a starting point but you can do:

Class.subclasses\_of(ActiveRecord::Base)

To get an array of the classes that subclass ActiveRecord::Base. Be  
warned that this might include classes outside of your code (like  
CGI::Session::ActiveRecordStore::Session from the looks of my test).  
Use ‘script/console’ to try it out to see what it gives you.

Matt
