SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

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

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app