# Rails attachment\_fu db\_file upload issue

**URL:** <https://www.ruby-forum.com/t/rails-attachment-fu-db-file-upload-issue/114370>\
**Category:** Rails\
**Created:** [September 11, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/rails-attachment-fu-db-file-upload-issue/114370 "2007-09-11T22:25:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jkahn](https://www.ruby-forum.com/u/jkahn)\
**Post date:** [September 11, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/rails-attachment-fu-db-file-upload-issue/114370/1 "2007-09-11T22:25:25Z")

</div>

I’ve recently added attachment\_fu to my applicatons, and it works  
without reporting any errors. Works fine in :file\_system mode, but  
not in :db\_file mode. It seems to upload only about 45 bytes of data  
instead of the entire image, image download works fine when I manually  
put the data in the database.

I’ve created an Icon model that has the following has\_attachment  
settings

has\_attachment :content\_type =\> :image,  
:storage =\> :db\_file,  
:max\_size =\> 1.megabyte,  
:resize =\> ‘75x75\>’,  
:processor =\> ‘MiniMagick’

validates\_as\_attachment

I’ve removed the resize and processor statements and I’m fairly  
confident that the issue is not related to image processing. And  
again the upload works to storage type :file\_system. My controller  
create method looks like this:

def create  
@icon = Icon.new(params[:icon])  
if @icon.save  
flash[:notice] = ‘Icon was successfully created.’  
redirect\_to :action =\> ‘show’, :id =\> @icon  
else  
render :action =\> :new  
end  
end

And my ‘new’ view looks like:

\<%= error\_messages\_for :icon %\>

\<% form\_for(:icon, :url =\> {:action =\> ‘create’},  
:html =\> { :multipart =\> true }) do |f| -%\>

Upload An Icon \<%= f.file\_field :uploaded\_data %\>

\<%= submit\_tag 'Create' %\>

\<% end -%\>

Any suggestions would be greatly appreciated.

Thanks,  
j

---

<div class="post-metadata">

**Author:** ![jkahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jkahn](https://www.ruby-forum.com/u/jkahn)\
**Post date:** [September 12, 2007, 8:06am UTC](https://www.ruby-forum.com/t/rails-attachment-fu-db-file-upload-issue/114370/2 "2007-09-12T08:06:14Z")

</div>

In your migrations for db\_file use  
t.column :binary, :limit =\> 500.megabyte

You can us whatever the limit you want to keep

That should fix it

---

<div class="post-metadata">

**Author:** ![jkahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jkahn](https://www.ruby-forum.com/u/jkahn)\
**Post date:** [October 22, 2007, 8:48am UTC](https://www.ruby-forum.com/t/rails-attachment-fu-db-file-upload-issue/114370/3 "2007-10-22T08:48:56Z")

</div>

Joshua,

Don’t know if you found the solution for this issue, but I thought I’d  
throw in my thoughts just in case. I’ve been having the exact same  
issue (corrupted uploads in the database with only 5 - 120 bytes, but  
full functionality using file\_system mode), and I came across the  
following thread:

[http://www.ruby-forum.com/topic/99870](http://www.ruby-forum.com/topic/99870)

A posting by ‘Adam’ near the end of the thread looks like this:

* * *

To get things working with S3 and windows, I changed:

```
  def temp_data
    if save_attachment?
      f = File.new( temp_path )
      f.binmode
      return f.read
    else
      return nil
    end
  end

```

(before, the method body was save\_attachment? ?  
File.read( temp\_path ) :  
nil )

It’s kind of ugly as it assumes attachments are binary, but it works  
in  
my situation.

* * *

I made this change and everything works properly now. Hooray!

~Mike
