AASM + interning empty string error

Second time I've run into this in the past week, so I thought I'd share it with you guys. I fumbled for a short while with the extremely unhelpful error message "interning empty string error" after a re-deployment to my staging server. It seemed to be affecting all of my pages, and I couldn't really load any page.

The Problem

This blog post is valid for rails 2.1.2 and rubyist-aasm 2.0.5. It may apply to other version combinations, but I make no guarantees.

First, let's dive into the error itself. This error message occurs when you call to_sym on an empty string:

john-mbp:trunk john$ irb
>> "".to_sym
ArgumentError: interning empty string
	from (irb):1:in `to_sym'
	from (irb):1

Now that we know what's up, let's dig into my stack trace.

/opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/persistence/active_record_persistence.rb:233:in to\_sym' /opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/persistence/active\_record\_persistence.rb:233:in aasm_read_state'
/opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/persistence/active_record_persistence.rb:135:in aasm\_current\_state' /opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/aasm.rb:50:in disabled?'
/var/vhosts/discovered/releases/20090213213806/app/helpers/base_admin_helper.rb:4:in disable\_link' /var/vhosts/discovered/releases/20090213213806/app/views/schools/index.rhtml:16:in _run_erb_47app47views47schools47index46rhtml'
/var/vhosts/discovered/releases/20090213213806/app/views/schools/index.rhtml:12:in each' /var/vhosts/discovered/releases/20090213213806/app/views/schools/index.rhtml:12:in _run_erb_47app47views47schools47index46rhtml'
...
...
...

The cause of this bug is the fact that my newest build added an aasm_state column to the School model, but my migration did not properly default schools to a particular state. As such, when an instance of the model was asked for its state, the AASM codebase raised an error. And this occurred on every page because I was rendering a link to a school in a common layout.

The Solution

After the fact, in SQL:

UPDATE schools SET aasm_state = 'active';

Or better yet, before the fact, in my migration:

def self.up
  add_column :schools, :aasm_state, :string
  School.reset_column_information
  School.update_all("aasm_state", "active")
end

The Lesson

Don't forget that there's a difference between a migration for a yet-to-be-deployed application and a live running application. Live apps require you to take into account that data already exists.