When Could AR Objects Not Have Their Methods?

I’m seeing something strange and so far haven’t been able to figure
it out. First off, here’s our setup:

MacOS X Tiger 10.4.10 (Intel)
Ruby 1.8.6
Rails 1.2.3
PostgreSQL 8.2.3

I have a ton of apps that work great but a brand new app is giving me
trouble.

I am getting an error indicating that the ‘errors’ method for the
object doesn’t exist. It should be inherited from the base AR class.

This app has a single model, ‘Upload’ that uses attachment_fu to take
uploads.

Here’s my code:

Model:

class Upload < ActiveRecord::Base
has_attachment :storage => :file_system, :max_size => 5.megabytes
end

Controller:

def new
@upload = Upload.new if @upload.blank?
end

View:

<% form_for :upload, @upload, :url => {:action => :create} do |form| %>
<% if @upload != nil && !@upload.errors.empty? -%>
<%= error_messages_for ‘upload’ %>
<% end -%>
<% end %>

The error I get is:

ActionView::TemplateError (undefined method `errors’ for #<Upload:
0x34e5e14>) on line #12 of app/views/uploads/new.rhtml:

12: <% if @upload != nil && !@upload.errors.empty? -%>
14: <%= error_messages_for ‘upload’ %>
15: <% end -%>

 #{RAILS_ROOT}/app/views/uploads/new.rhtml:12:in

_run_rhtml_47app47views47uploads47new46rhtml' /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/ action_view/helpers/form_helper.rb:151:infields_for’
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/
action_view/helpers/form_helper.rb:127:in form_for' #{RAILS_ROOT}/app/views/uploads/new.rhtml:4:in_run_rhtml_47app47views47uploads47new46rhtml’

Huh?

When I put in some debugging code, it gets even more interesting:

I added this code to the controller:

 logger.debug "Upload Class: #{@upload.class}"
 logger.debug @upload.methods

Ok, this should tell me more. Output:

Upload Class: Upload

inspectinstance_execlistenerclonemethodrequire_gemrequire_library_or_gem
request_progresspublic_methodsencode64blank?
listener=instance_variable_defined?decode64equal?
freezeunloadableloadb64encodemethodssilence_warningsrespond_to?
enable_warningsreturningremove_subclasses_ofdupto_yamlinstance_variables
optionsdecode_b__id__object_ideql?
requireidprocesssilence_streamrequest_beginssingleton_methodsto_yaml_sty
lesendwith_optionstaintfrozen?instance_variable_getgeminstance_of?
__send__to_aextend_with_included_modules_fromrequest_notify`typeinstance
_evalprotected_methodsto_yaml_propertiesdisplay=====subclasses_ofinstanc
e_variable_setsilence_stderrextended_byextendkind_of?
to_sdaemonizeinstance_valuesclasshashto_paramprivate_methods=~tainted?
suppresstagurito_jsonuntaintnil?
copy_instance_variables_fromtaguri=dcloneis_a?

So, it is the right class but the methods are wrong.

What the heck is going on here? When I create a new Upload object
from the console, everything works properly and the methods are all
there.

What gives?

Thanks,
Hunter

@upload = Upload.new if @upload.blank?

the if statement is the most likely cause

the if statement is the most likely cause

it should always return false or throw an error if @upload is undefined.
I assume this is for if new is called from edit/update were @upload is
already defined.

try “unless @upload” instead

Thanks for responding.

I tried changing it to:

@upload = Upload.new unless @upload

but I’m getting the same error.

In the view, I removed the conditional around the error messages
helper output and that didn’t help either.

It’s strange - it’s like this instance of the ‘Upload’ class isn’t
properly hooking up with AR - it’s not getting all of the inherited
methods… But when used in the console it works fine.

This project has nothing in it but the ‘attachment_fu’ plugin. Can
anyone think of any other code that would be loaded here that could
somehow screw up the class loading?

Hi –

On Thu, 16 Aug 2007, Hunter H. wrote:

trouble.

View:
0x34e5e14>) on line #12 of app/views/uploads/new.rhtml:

12: <% if @upload != nil && !@upload.errors.empty? -%>
14: <%= error_messages_for ‘upload’ %>
15: <% end -%>

There’s no line 13. Are you superstitious? :slight_smile:

I don’t know what the problem is, but I’m curious what would happen if
you simply removed the surrounding lines and just had line 14.
error_messages_for is smart enough not to bother showing anything if
there’s nothing to show.

David

I was just thinking that so I tried it…

Using another class name does seem to work, though I can’t find
‘Upload’ in any reserved word list of mentioned at all in attachment_fu.

For this one I’ll probably not worry about it and name it something
else but I’m surprised I hit this issue as I did check before hand.

Hi –

On Thu, 16 Aug 2007, Hunter H. wrote:

helper output and that didn’t help either.

It’s strange - it’s like this instance of the ‘Upload’ class isn’t
properly hooking up with AR - it’s not getting all of the inherited
methods… But when used in the console it works fine.

This project has nothing in it but the ‘attachment_fu’ plugin. Can
anyone think of any other code that would be loaded here that could
somehow screw up the class loading?

You might try changing the name of Upload to Blah or something, just
to see if it’s a namespace thing, though I don’t think attachment_fu
defines an Upload class. And I assume you’ve checked and
double-checked that Upload is inheriting from AR::Base – that would
be the other issue.

David