While beefing up the admin backend of a Rails app that uses ActiveScaffold for its admin UI, I encountered an error that only happened in production and the stack trace wasn’t much help in identifying the cause.
After failing to recreate the problem with a (relatively) small test case, I took the opposite approach and began ripping code out of the original application until the failure went away.
Background
The problem began when I tried to access a nested scaffold from its parent scaffold. The relevant models are as follows:
1 2 3 |
class Country < ActiveRecord::Base has_many :regions end |
1 2 3 |
class Region < ActiveRecord::Base belongs_to :country end |
The Problem
When viewing the list of countries, I should have been able to click the regions column and get a nested list of regions in the country like so.

Except, this is what I got.

And the log file had the following stack trace.
NoMethodError (undefined method `controller_name' for nil:NilClass):
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/caching/sweeping.rb:83:in `callback'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/caching/sweeping.rb:65:in `after'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:208:in `around_proc'
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:182:in `call'
/usr/lib64/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/callbacks.rb:182:in `evaluate_method'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:184:in `call'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:635:in `run_before_filters'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:615:in `call_filters'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib64/ruby/1.8/benchmark.rb:293:in `measure'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/rescue.rb:136:in `perform_action_without_caching'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
/usr/lib64/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/query_cache.rb:8:in `cache'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:524:in `send'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:524:in `process_without_filters'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/filters.rb:606:in `process_without_session_management_support'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/session_management.rb:134:in `process'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb:392:in `process'
/usr/lib64/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:183:in `handle_request'
...
So the stack trace is pointing to a caching problem, which explains why the problem did not occur in development mode. But that’s about all the stack trace revealed to me.
Another issue is that this same code worked fine in a couple other Rails applications. However, none of the applications used identical sets of gems and plugins so there was likely a problem introduced by some combination of external dependencies.
The Fix
After having failed to recreate the problem in a small test case, it was time to start ripping the code apart.
As I pulled more and more functionality out of the application, I finally got it working when I ripped out the acts_as_audited plugin. A quick google took me to this post that offered the solution. After applying that fix, the nested scaffolds worked as expected, and after reverting all the other changes no other problems were raised.

