Ideally in a Rails app, you’ll have extensive unit tests for all your model classes. Those tests will be updated and run before everyone deploys. And you get a pony, to make getting to the office easier. So, when you have a huge stack of untested models that are constantly being updated, is there a quick way to test them and make sure that at least there aren’t any syntax errors or that kind of thing? Well, here’s my 80% solution.

describe ActiveRecord::Base do
  it "should load all model classes and not throw exceptions" do
    models = []
    Dir["#{RAILS_ROOT}/app/models/*"].each do |file|
       matches = File.read(file).scan /^\s*class (\w+) < ActiveRecord::Base/
       matches.each do |match_data|
         match_data.each {|match| models << match unless models.include?(match) }
       end
    end
    models.each {|model| eval model }
  end
end

This is an Rspec test that finds all your model classes and loads them, kind of like Rails does when loaded in a production environment. That way, if there are any obvious take-down-the-site level screw-ups they’ll be pointed out to you in short order. Combine that with a continuous build server that blocks your deploys and you can get some pretty decent insurance that your server will restart successfully.

You may have to make some modifications to the regex if you’re using STI or other inheritance mechanisms. I had to put in the check for inheriting from ActiveRecord::Base in order to prevent it from trying to instantiate internal classes like custom exceptions without properly namespacing them.