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? -- Cheers, Hasan Diwan <hasan.diwan@gmail.com>
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 Diwan <hasan.diwan@gmail.com> 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? > -- > Cheers, > Hasan Diwan <hasan.diwan@gmail.com> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Kind regards, Nathaniel Brown President & CEO Inimit Innovations Inc. - http://inimit.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060730/5e5332c0/attachment.html
On 7/29/06, Hasan Diwan <hasan.diwan@gmail.com> 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
On 01/08/06, Matthew Denner <matt.denner@gmail.com> wrote:> > 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. >>> Class.subclasses_of(ActiveRecord::Base)=> [CGI::Session::ActiveRecordStore::Session] There are other subclasses of ActiveRecord::Base in this project. -- Cheers, Hasan Diwan <hasan.diwan@gmail.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060908/e30bfc04/attachment-0001.html