undefined local variable or method `should_act_as_list’

I got this error today, which was a surprise because I was porting over good, tested code from an old version of an application. To recreate the error after solving, I built a simple test app with a single model, SantasList. The test file looks like this:

require 'test_helper'

class SantasListTest < ActiveSupport::TestCase

At first I suspected a missing gem, but environments/test.rb showed this:

  config.gem 'thoughtbot-shoulda', :lib => 'shoulda', :source => 'http://gems.github.com'
  config.gem 'seanhussey-woulda', :lib => 'woulda', :source => 'http://gems.github.com'

I half suspected that woulda wasn’t loading properly, and I was half right. I started to trace the problem in the gem itself, which I’d wisely unpacked into the application. Inside the woulda gem, I found the contents of shoulda_macros/woulda_macros.rb:

require 'woulda'

Simple enough. So in lib/woulda.rb I found the line that loads the acts_as_list macro:

  require "#{woulda_dir}/acts_as_list" if defined? ActiveRecord::Acts::List

Voila!* First, looking at the whole file shows that the author was smart, and only loads the macros that might actually be used. Second, I’ve found my problem: I don’t have the acts_as_list plugin installed! No plugin, no macro. So I was half right – the macro wasn’t loading, but it was my fault. As I said, I’m porting the application to a new code base, and I hadn’t needed this plugin yet. I’m thrilled the problem was so easy to find, and Sean Hussey’s clean code certainly helped.

As a side pitch, if you’re not using woulda yet, I highly recommend it. For shoulda users, it provides macros for the most common ActiveRecord plugins like acts_as_list, acts_as_taggable, will_paginate, paperclip, acts_as_state_machine, and more. And if you’re not familiar with shoulda macros, then why not??

*I like to say “viola” as a joke, but in print it just looks like a typo.

Tags: , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: