# Uploading a file and working with 2 models

**URL:** <https://www.ruby-forum.com/t/uploading-a-file-and-working-with-2-models/173376>\
**Category:** Rails\
**Created:** [August 7, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/uploading-a-file-and-working-with-2-models/173376 "2009-08-07T16:37:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwhiten1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@rwhiten1](https://www.ruby-forum.com/u/rwhiten1)\
**Post date:** [August 7, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/uploading-a-file-and-working-with-2-models/173376/1 "2009-08-07T16:37:40Z")

</div>

I have an app that engages physical files of several types. Each type  
has different metadata associated, so i’ve broken them out in  
different models. To keep thing simple, I’ll just deal use images in  
my example.

I have an images table (with an Image model) that stores fuzzy info  
like title, description, rating, etc. The Image model belongs\_to a  
Binary model that is backended by a binaries table with physical file  
metadata (name, extension, size, MIME Type, etc.). My Binary model is  
_only_ a model. There’s no controller or set of views because I really  
want all interaction to occur through the typed model.

I have an Images controller with the relevant views and I’d like to  
accept an upload there, but then delegate to the Binary model for  
handling everything related to the physical file itself (uploading,  
extracting properties, saving to a new location, etc.). I can’t seem  
to get that to work and my Ruby/Rails n00b-ness isn’t helping me at  
all. 🙂 Some of my syntax may simply be incorrect - I’m struggling  
with the Ruby idioms, coming from a long history of C-style syntax. In  
many cases, I’ve tried several variations, but I’m just going to paste  
what I have now. In other cases, I’m including snippets of code that  
I’ve seen elsewhere, but don’t completely understand yet  
(attr\_accessor is a good example).

I’m hoping someone here can help point me in the right direction. The  
error that I’m currently getting is:

undefined method `original\_path’ for “online-resume-req.txt”:String

It seems that the upload, in its entirely, is never making it to the  
server. I’m only getting the file name as a string. Code follows:

ImagesController::create()

def create  
@image = Image.new(params[:image])  
@binary = Binary.new

```
if ( @binary.upload(params[:image][:uploaded_file]) )
  #do stuff
end

```

end

views/images/\_form.html.erb

\<% form\_for( @image, :multipart =\> true ) do |f| %\>  
\<%= f.error\_messages -%\>

\<%= label\_tag( 'image\_uploaded\_file', 'File' ) -%\> \<%= file\_field( :image, :uploaded\_file ) -%\>

\<%= f.label :name %\> \<%= f.text\_field :name %\>

\<%= f.label :description %\> \<%= f.text\_area( :description ) %\>

\<%= f.submit( 'Upload' ) %\>

\<% end %\>

Image model:

class Image \< ActiveRecord::Base  
belongs\_to :binary

validates\_presence\_of( :uploaded\_file )\

attr\_accessor :uploaded\_file  
end

Binary model

class Binary \< ActiveRecord::Base  
has\_one :image

def upload(uploaded\_file)  
logger.info( 'Uploading: ’ + uploaded\_file.to\_json )

```
self.name = uploaded_file.original_path
self.mime_type = uploaded_file.content_type
save_as = Rails.root.join( 'public', 'bin', '_tmp',

```

uploaded\_file.original\_path )

```
File.open( save_as, 'w' ) do |file|
  file.write( uploaded_file.read )
end

self.size = File.size( "#{save_as}" )

logger.info( 'Set file size to ' + self.size.to_s )

self.save

logger.info( 'Stored' );

```

end  
end

---

<div class="post-metadata">

**Author:** ![rwhiten1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@rwhiten1](https://www.ruby-forum.com/u/rwhiten1)\
**Post date:** [August 7, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/uploading-a-file-and-working-with-2-models/173376/2 "2009-08-07T16:47:37Z")

</div>

So I realized that somewhere along the way, I had altered my  
“form\_for” call in such a way that it stopped posting the form in  
multipart. That’s been corrected. The file is now being uploaded, but  
it’s being written as a 0B file. That’s what I was getting yesterday  
when I started making wholesale changes to figure out what was  
happening.

Any idea why my file.write() call is screwed up?

Thanks.
