Uploading a file and working with 2 models

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. :slight_smile: 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

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.