# Attachment\_fu, PDF files and :db\_file storage type

**URL:** <https://www.ruby-forum.com/t/attachment-fu-pdf-files-and-db-file-storage-type/114078>\
**Category:** Rails\
**Created:** [September 7, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/attachment-fu-pdf-files-and-db-file-storage-type/114078 "2007-09-07T17:25:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gattox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@gattox](https://www.ruby-forum.com/u/gattox)\
**Post date:** [September 7, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/attachment-fu-pdf-files-and-db-file-storage-type/114078/1 "2007-09-07T17:25:36Z")

</div>

Problem:

- correctly uploading PDF files, PNG, JPEG and storing in MySQL dbase;
- when downloading (and it seems only with PDF files) with send\_data and  
saving on file system, consistently only 2kbyte of data are downloaded;  
if alternatively :disposition =\> ‘inline’ Adobe Reader signals incorrect  
file content.

Has anyone experienced the same problem and what can be wrong with the  
following?

My migrations:

class CreateDocmetas \< ActiveRecord::Migration  
def self.up  
create\_table :docmeta do |t|  
t.column :db\_file\_id, :integer  
t.column :size, :integer, :null =\> false  
t.column :content\_type, :text, :null =\> false  
t.column :filename, :text, :null =\> false  
t.column :height, :integer  
t.column :width, :integer  
t.column :parent\_id, :integer  
t.column :thumbnail, :text  
end  
end

def self.down  
drop\_table :docmeta  
end  
end

class CreateDbFiles \< ActiveRecord::Migration  
def self.up  
create\_table :db\_file do |t|  
t.column :data, :binary  
end  
end

def self.down  
drop\_table :db\_file  
end  
end

My models:

class Docmeta \< ActiveRecord::Base  
set\_table\_name ‘docmeta’

belongs\_to :db\_file

has\_attachment :storage =\> :db\_file,  
:min\_size =\> 1.kilobyte,  
:max\_size =\> 10.megabytes

validates\_as\_attachment

end

class DbFile \< ActiveRecord::Base  
set\_table\_name ‘db\_file’  
end

My controller (only for the download action):

def view  
@attachment = Attachment.find(params[:attchid])  
if @attachment  
send\_data @attachment.docmeta.db\_file.data,  
:filename =\> @attachment.docmeta.filename,  
:type =\> @attachment.docmeta.content\_type,  
:disposition =\> ‘attachment’  
end  
end

---

<div class="post-metadata">

**Author:** ![gattox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@gattox](https://www.ruby-forum.com/u/gattox)\
**Post date:** [September 8, 2007, 5:44am UTC](https://www.ruby-forum.com/t/attachment-fu-pdf-files-and-db-file-storage-type/114078/2 "2007-09-08T05:44:21Z")

</div>

When you create a column with binary file type, Rails automatically  
converts it to mysql type blob. By default blob type is 2kb. Use  
t.column data, :binary, :limit =\> 10.megabyte  
in your migration to set the correct size of the mysql blob.

- Tushar

On Sep 7, 8:25 am, Roberto Gattinoni \<rails-mailing-l…@andreas-
