Other languages: 日本語
We have a simple application that doesn’t have an
ActiveRecord dependency. It’s deployed to Heroku, and it’s been working fine on Rails 3.0.x since April 2011. We knew we weren’t using
ActiveRecord for database connectivity, but we let it be, since it wasn’t causing any issues.
When upgrading to Rails 3.1, we found that every single page would give
ActiveRecord::ConnectionNotEstablished on our staging environment on Heroku. The same error didn’t happen in development. Although we might have been able to get
gem 'pg' set up and working, we really didn’t need an ActiveRecord dependency at all.
I found part of a solution on StackOverflow, but it needed a little tweaking for Rails 3.1.0.
# File: config/application.rb # Pick the frameworks you want: # require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" require "active_resource/railtie" require "sprockets/railtie" require "rails/test_unit/railtie"
That’s what Rails 3.1.0 generates when running
rails new myproject --skip-active-record. (Note that
require "active_record/railtie" is commented out.) This solved our
ActiveRecord::ConnectionNotEstablished problem, but gave us a few others, namely:
ActiveRecord::RecordNotFound(used for 404s)
There’s some normal stuff to get rid of in terms of
test_helper.rb. Here’s an example:
# File spec/spec_helper.rb # # If you're not using ActiveRecord, or you'd prefer not to run each of your # # examples within a transaction, remove the following line or assign false # # instead of true. # config.use_transactional_fixtures = true
You may have others. Tarantula had to be adjusted for us, for example.
ActiveRecord::RecordNotFound, however, was an interesting problem. Everything worked fine without
ActiveRecord except the places where we were using
ActiveRecord::RecordNotFound to give a
HTTP 404 to the user agent. That seems strange in a lot of ways, because a
404 shouldn’t have anything to do with your chosen ORM. My first intuition was to do
require 'active_record/errors' (see also the Rails docs), but that caused problems with assumptions in
Right now, the below is what we ended up with:
# File: config/application.rb # Pick the frameworks you want: # require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" require "active_resource/railtie" require "sprockets/railtie" require "rails/test_unit/railtie" # For errors like ActiveRecord::RecordNotFound require "active_record"
Our automated tests (Rspec, integration tests, Tarantula, Selenium, etc) all pass with it and we no longer get
ActiveRecord::ConnectionNotEstablished, but we still have an ActiveRecord dependency I don’t like. (There must be another error we can raise – I don’t entirely like the
render '/404.html', status: 404 solution for several reasons.)
This blog post has been translated/incorporated into a Japanese blog post.
To solve this error this site is helpful. http://t.co/UXFUJrO6
— Saka (@iwhurtafly) February 5, 2012