Patrick Ritchie Patrick Ritchie - 3 months ago 15
Ruby Question

How to fix / debug 'expected x.rb to define X.rb' in Rails

I have seen this problem arise in many different circumstances and would like to get the best practices for fixing / debugging it on StackOverflow.

To use a real world example this occurred to me this morning:

expected announcement.rb to define Announcement

The class worked fine in development, testing and from a production console, but failed from in a production Mongrel. Here's the class:

class Announcement < ActiveRecord::Base
has_attachment :content_type => 'audio/mp3', :storage => :s3

The issue I would like addressed in the answers is not so much solving this specific problem, but how to properly debug to get Rails to give you a meaningful error as expected x.rb to define X.rb' is often a red herring...

Edit (3 great responses so far, each w/ a partial solution)


  1. From Joe Van Dyk: Try accessing the model via a console on the environment / instance that is causing the error (in the case above: script/console production then type in 'Announcement'.

  2. From Otto: Try setting a minimal plugin set via an initializer, eg: config.plugins = [ :exception_notification, :ssl_requirement, :all ] then re-enable one at a time.

Specific causes:

  1. From Ian Terrell: if you're using attachment_fu make sure you have the correct image processor installed. attachment_fu will require it even if you aren't attaching an image.

  2. From Otto: make sure you didn't name a model that conflicts with a built-in Rails class, eg: Request.

  3. From Josh Lewis: make sure you don't have duplicated class or module names somewhere in your application (or Gem list).


That is a tricky one.

What generally works for me is to run "script/console production" on the production server, and type in:


That will usually give you a better error message. But you said you already tried that?