views:

762

answers:

5

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
end

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)

Debugging:

  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.

+2  A: 

That is a tricky one.

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

Announcement

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

Joe Van Dyk
Good start, worth an up vote. But how do you debug the case where the error is only occurring via Mongrel?
Patrick Ritchie
+2  A: 

I've encountered this before, and the AttachmentFu plugin was to blame. I believe in my case it was due to AttachmentFu expecting a different image processor than what was available, or non-supported versions were also installed. The problem was solved when I explicitly added :with => :rmagick (or similar -- I was using RMagick) to the has_attachment method call even for non-image attachments. Obviously, make sure that your production environment has all the right gems (or freeze them into your application) and supporting software (ImageMagick) installed. YMMV.

As for not getting Rails and AttachmentFu to suck up and hide the real error -- we fixed it before figuring it out completely.

Ian Terrell
Good extra info, but not a complete answer... I believe using joevandyk's technique of testing from a console would have given you a better error message.
Patrick Ritchie
+1  A: 

You can try disabling all your plugins and add them back in one by one.

In environment.rb in the Initalizer section, add a line like this one:

config.plugins = [ :exception_notification, :ssl_requirement, :all ]

Start with the minimum set to run your application and add them in one by one. I usually get this error when I've defined a model that happens to map to an existing filename. For example, a Request model but Rails already has a request.rb that gets loaded first.

Otto
A: 

Did you try it in script/console production? Did it give you any better errors?

Ryan Bigg
As stated in the initial question, everything worked fine in a production console.
Patrick Ritchie
+1  A: 

I had this problem for a while and in my case the error was always preceded from this S3 error:

(AWS::S3::Operation Aborted) "A conflicting conditional operation is currently in progress against this resource. Please try again."

This problem usually occurs when creating the same bucket over and over again. (Source AWS Developers forum)

This was due to the fact that I had used attachment_fu to create the bucket and I had decommented the line containing the command Bucket.create(@@bucket_name) in lib/technoweenie/attachment_fu/backends/s3_backends.rb (near to line 152).

Once commented or deleted the command Bucket.create(@@bucket_name) the problem disappeared.

I hope this helps.

Tommaso