# When Could AR Objects Not Have Their Methods?

**URL:** <https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433>\
**Category:** Rails\
**Created:** [August 17, 2007, 12:57am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433 "2007-08-17T00:57:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [August 17, 2007, 12:57am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433/1 "2007-08-17T00:57:31Z")

</div>

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:in`fields\_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

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [August 17, 2007, 1:04am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433/2 "2007-08-17T01:04:06Z")

</div>

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

the if statement is the most likely cause

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [August 17, 2007, 1:06am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433/3 "2007-08-17T01:06:19Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [August 17, 2007, 2:06am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433/4 "2007-08-17T02:06:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [August 17, 2007, 1:35am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433/5 "2007-08-17T01:35:35Z")

</div>

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? 🙂

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

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [August 17, 2007, 2:22am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433/6 "2007-08-17T02:22:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [August 17, 2007, 2:15am UTC](https://www.ruby-forum.com/t/when-could-ar-objects-not-have-their-methods/196433/7 "2007-08-17T02:15:38Z")

</div>

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

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
