VocabReminder
Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Load All ActiveRecord::Base Model Classes in Rails Application

       

Here is a simple rake task which will instantiate all of your Active Record models, provided that they are located in the RAILS_ROOT/app/models directory. Interestingly, all plugin models are instantiated by default when you run the task, for instance, if you are using the Acts As Taggable On plugin, you have access to Tag, Tagging without having to include the plugin models directory path to the task.


namespace :load_ar do
    desc "load up all active record models"
    task :models => :environment do
      models = ActiveRecord::Base.send(:subclasses)
      Dir["#{RAILS_ROOT}/app/models/"].each do |file|
         model = File.basename(file, ".").classify
         models << model unless models.include?(model)
      end
   end
end
If your're in the console, you can get all the load paths for your Active Record models with the following from the API.
 
Rails.configuration.load_paths.each do |path|
   p path
end

Tagged w/ #active record #classify #constants #initialize #load #models #rake #ruby on railsprogramming #ruby on rails