Engines question

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?

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. [email protected] wrote:


Cheers,
Hasan D. [email protected]


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Kind regards,

Nathaniel B.
President & CEO
Inimit Innovations Inc. - http://inimit.com

On 7/29/06, Hasan D. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs