Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Ruby/JavaScript Developer at Liaison. Previously, I was a Developer at Continuity and Hedgeye, a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize TechCorridor.io, ICRuby, OpenHack Iowa City, and previously organized NewHaven.rb. I have an amazing wife named Danielle Oakes.

Filtering for the month September, 2011. Clear

ActiveRecord::ConnectionNotEstablished in Rails 3.1 on Heroku

by Ben

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:

Fixtures

There’s some normal stuff to get rid of in terms of spec_helper.rb and/or 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

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 'rspec/rails'.

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.)

Update:

This blog post has been translated/incorporated into a Japanese blog post.

Reactions

relative_time_ago in Padrino should be time_ago_in_words

by Ben

You might find relative_time_ago referenced in the Padrino Guides or Padrino Helpers RDocs, but that doesn’t exist.

According to the Application Helpers guides, you probably want distance_of_time_in_words or time_ago_in_words.

Example:

%p
  = time_ago_in_words(my_date)
  ago

For reasons I don’t quite understand, this documentation is also incorrect:

time_ago_in_words(2.days.ago) # => "2 days ago"

The actual behavior is:

time_ago_in_words(2.days.ago) # => "2 days"

I’m planning to move some of this into the padrino-framework project directly. Incorrect documentation is worse than no documentation; maybe it will be more accurate if kept in the code. :)

Accessing Cookies in Padrino/Sinatra

by Ben

Unlike in Rails, there is no global cookies object in Padrino or Sinatra. Instead, you need to use request.cookies within your request handler:

get :index do
  my_value = request.cookies['my_value']
end

Enforcing SSL in Padrino

by Ben

We’re trying rack-ssl-enforcer out on our Padrino-based project. It seems to work when configuring like so:


# File: app/app.rb
class MyApp < Padrino::Application
  # [...]
  configure :production do
    require 'rack-ssl-enforcer'
    use Rack::SslEnforcer
  end
end

We're using it with Padrino with some success. It's great that such a common need is really simple to set up. However, even after looking through the docs further, I'm surprised that I can request over HTTP and I don't get redirected to HTTPS. I've yet to figure out the issue, but it seems to meet our needs just fine otherwise.

This is in response to Dan Mayer's article Force all Sinatra traffic to https. I had started to comment, but his blog errored-out when submitting. Thanks for posting it, Dan!