# Problem uploading file to database mysql blob field

**URL:** <https://www.ruby-forum.com/t/problem-uploading-file-to-database-mysql-blob-field/172906>\
**Category:** Rails\
**Created:** [July 29, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/problem-uploading-file-to-database-mysql-blob-field/172906 "2009-07-29T19:53:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagarwal1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@sagarwal1981](https://www.ruby-forum.com/u/sagarwal1981)\
**Post date:** [July 29, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/problem-uploading-file-to-database-mysql-blob-field/172906/1 "2009-07-29T19:53:57Z")

</div>

Hi All,

Please help me ,I need to look into this issue asap, I need to change  
the redmine functionality of uploading files to disk to upload them to  
database. I tried suggestion on  
[http://dizzy.co.uk/ruby\_on\_rails/contents/store-file-uploads-in-database](http://dizzy.co.uk/ruby_on_rails/contents/store-file-uploads-in-database)  
but no luck , My form is multipart but still getting the error.

Please bare with me I am new to ruby and rails.

Here is the like for my redmine question  
[http://www.redmine.org/boards/2/topics/7737](http://www.redmine.org/boards/2/topics/7737)

I need to change Redmine functionality to upload files directly to  
database and not to local filesystem. \*

1. What I think is that I should add a field to “Attachments” table.\*  
I did this via this following migration  
class AddFileToAttachments \< ActiveRecord::Migration  
def self.up  
add\_column :“attachments”, :“file”, :binary, :limit =\>10.megabytes  
end end

def self.down

```
  drop_column :"attachments", :"file"

```

end

1. Now I should change the model code to copy the file content into the  
blob.

I updated attachment.rb , just added the line self.file =  
@temp\_file.read  
as I need to read the content of file and put it into database.

def file=(incoming\_file)  
unless incoming\_file.nil?  
@temp\_file = incoming\_file  
if @temp\_file.size \> 0  
self.filename = sanitize\_filename(@temp\_file.original\_filename)  
self.disk\_filename = Attachment.disk\_filename(filename)  
self.content\_type = @temp\_file.content\_type.to\_s.chomp  
self.filesize = @temp\_file.size  
self.file = @temp\_file.read  
end  
end  
end

But After doing so I am getting the following error: Anybody please give  
some direction

ndefined method `original\_filename’ for #String:0x8c09038

RAILS\_ROOT: C:/InstantRails/rails\_apps/redmine  
Application Trace | Framework Trace | Full Trace

app/models/attachment.rb:56:in `file=' app/models/attachment.rb:60:in `file=’  
app/controllers/application.rb:183:in `attach_files' app/controllers/application.rb:180:in `each\_value’  
app/controllers/application.rb:180:in `attach_files' app/controllers/projects_controller.rb:195:in `add\_file’

vendor/rails/activerecord/lib/active\_record/base.rb:2372:in `send' vendor/rails/activerecord/lib/active_record/base.rb:2372:in `attributes=’  
vendor/rails/activerecord/lib/active\_record/base.rb:2371:in `each' vendor/rails/activerecord/lib/active_record/base.rb:2371:in `attributes=’  
vendor/rails/activerecord/lib/active\_record/base.rb:2141:in `initialize' vendor/rails/activerecord/lib/active_record/base.rb:642:in `new’  
vendor/rails/activerecord/lib/active\_record/base.rb:642:in `create' vendor/rails/actionpack/lib/action_controller/base.rb:1166:in `send’  
vendor/rails/actionpack/lib/action\_controller/base.rb:1166:in  
`perform_action_without_filters' vendor/rails/actionpack/lib/action_controller/filters.rb:579:in `call\_filters’  
vendor/rails/actionpack/lib/action\_controller/filters.rb:572:in  
`perform_action_without_benchmark' vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform\_action\_without\_rescue’  
C:/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform\_action\_without\_rescue’  
vendor/rails/actionpack/lib/action\_controller/rescue.rb:201:in  
`perform_action_without_caching' vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform\_action’  
vendor/rails/activerecord/lib/active\_record/connection\_adapters/abstract/query\_cache.rb:33:in  
`cache' vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache’  
vendor/rails/actionpack/lib/action\_controller/caching/sql\_cache.rb:12:in  
`perform_action' vendor/rails/actionpack/lib/action_controller/base.rb:529:in `send’  
vendor/rails/actionpack/lib/action\_controller/base.rb:529:in  
`process_without_filters' vendor/rails/actionpack/lib/action_controller/filters.rb:568:in `process\_without\_session\_management\_support’  
vendor/rails/actionpack/lib/action\_controller/session\_management.rb:130:in  
`process' vendor/rails/actionpack/lib/action_controller/base.rb:389:in `process’  
vendor/rails/actionpack/lib/action\_controller/dispatcher.rb:149:in  
`handle_request' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:107:in `dispatch’  
vendor/rails/actionpack/lib/action\_controller/dispatcher.rb:104:in  
`synchronize' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:104:in `dispatch’  
vendor/rails/actionpack/lib/action\_controller/dispatcher.rb:120:in  
`dispatch_cgi' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:35:in `dispatch’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:76:in  
`process' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:74:in `synchronize’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:74:in  
`process' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:159:in `process\_client’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:in  
`each' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:in `process\_client’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in `initialize’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in  
`new' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in  
`initialize' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in `new’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:282:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:in  
`each' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel\_rails:128:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/command.rb:212:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel\_rails:281  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:503:in  
`load' vendor/rails/activesupport/lib/active_support/dependencies.rb:503:in `load’  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:355:in  
`new_constants_in' vendor/rails/activesupport/lib/active_support/dependencies.rb:503:in `load’  
vendor/rails/railties/lib/commands/servers/mongrel.rb:64  
C:/InstantRails/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:31:in  
`gem_original_require' C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:510:in  
`require' vendor/rails/activesupport/lib/active_support/dependencies.rb:355:in `new\_constants\_in’  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:510:in  
`require' vendor/rails/railties/lib/commands/server.rb:39 C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem\_original\_require’  
C:/InstantRails/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:31:in  
`require’  
script/server:3

app/models/attachment.rb:56:in `file=' app/models/attachment.rb:60:in `file=’  
vendor/rails/activerecord/lib/active\_record/base.rb:2372:in `send' vendor/rails/activerecord/lib/active_record/base.rb:2372:in `attributes=’  
vendor/rails/activerecord/lib/active\_record/base.rb:2371:in `each' vendor/rails/activerecord/lib/active_record/base.rb:2371:in `attributes=’  
vendor/rails/activerecord/lib/active\_record/base.rb:2141:in `initialize' vendor/rails/activerecord/lib/active_record/base.rb:642:in `new’  
vendor/rails/activerecord/lib/active\_record/base.rb:642:in `create' app/controllers/application.rb:183:in `attach\_files’  
app/controllers/application.rb:180:in `each_value' app/controllers/application.rb:180:in `attach\_files’  
app/controllers/projects\_controller.rb:195:in `add_file' vendor/rails/actionpack/lib/action_controller/base.rb:1166:in `send’  
vendor/rails/actionpack/lib/action\_controller/base.rb:1166:in  
`perform_action_without_filters' vendor/rails/actionpack/lib/action_controller/filters.rb:579:in `call\_filters’  
vendor/rails/actionpack/lib/action\_controller/filters.rb:572:in  
`perform_action_without_benchmark' vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform\_action\_without\_rescue’  
C:/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform\_action\_without\_rescue’  
vendor/rails/actionpack/lib/action\_controller/rescue.rb:201:in  
`perform_action_without_caching' vendor/rails/actionpack/lib/action_controller/caching/sql_cache.rb:13:in `perform\_action’  
vendor/rails/activerecord/lib/active\_record/connection\_adapters/abstract/query\_cache.rb:33:in  
`cache' vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache’  
vendor/rails/actionpack/lib/action\_controller/caching/sql\_cache.rb:12:in  
`perform_action' vendor/rails/actionpack/lib/action_controller/base.rb:529:in `send’  
vendor/rails/actionpack/lib/action\_controller/base.rb:529:in  
`process_without_filters' vendor/rails/actionpack/lib/action_controller/filters.rb:568:in `process\_without\_session\_management\_support’  
vendor/rails/actionpack/lib/action\_controller/session\_management.rb:130:in  
`process' vendor/rails/actionpack/lib/action_controller/base.rb:389:in `process’  
vendor/rails/actionpack/lib/action\_controller/dispatcher.rb:149:in  
`handle_request' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:107:in `dispatch’  
vendor/rails/actionpack/lib/action\_controller/dispatcher.rb:104:in  
`synchronize' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:104:in `dispatch’  
vendor/rails/actionpack/lib/action\_controller/dispatcher.rb:120:in  
`dispatch_cgi' vendor/rails/actionpack/lib/action_controller/dispatcher.rb:35:in `dispatch’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:76:in  
`process' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:74:in `synchronize’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/rails.rb:74:in  
`process' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:159:in `process\_client’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:in  
`each' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:158:in `process\_client’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in `initialize’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in  
`new' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:285:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in  
`initialize' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in `new’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel.rb:268:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:282:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:in  
`each' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/configurator.rb:281:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel\_rails:128:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/mongrel/command.rb:212:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/mongrel\_rails:281  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:503:in  
`load' vendor/rails/activesupport/lib/active_support/dependencies.rb:503:in `load’  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:355:in  
`new_constants_in' vendor/rails/activesupport/lib/active_support/dependencies.rb:503:in `load’  
vendor/rails/railties/lib/commands/servers/mongrel.rb:64  
C:/InstantRails/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:31:in  
`gem_original_require' C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:510:in  
`require' vendor/rails/activesupport/lib/active_support/dependencies.rb:355:in `new\_constants\_in’  
vendor/rails/activesupport/lib/active\_support/dependencies.rb:510:in  
`require' vendor/rails/railties/lib/commands/server.rb:39 C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem\_original\_require’  
C:/InstantRails/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:31:in  
`require’  
script/server:3

Request

Parameters:

{“commit”=\>“Add”,  
“id”=\>“saur”,  
“attachments”=\>{“1”=\>{“description”=\>“ss”,  
“file”=\>#\<File:C:/DOCUME~1/sauragar/LOCALS~1/Temp/CGI3868-0\>}}}

Show session dump

* * *

projectslist\_files\_sort:  
:order: asc  
🔑 filename  
:user\_id: 1  
adminprojects\_sort:  
:order: asc  
🔑 name  
issuesindex\_sort:  
:order: desc  
🔑 id  
flash: !map:ActionController::Flash::FlashHash {}

:query:  
:project\_id: 1  
:filters:  
tracker\_id:  
:values:

- “1”  
:operator: “=”  
status\_id:  
:values:
- “”  
:operator: o

Response

Headers:

{“cookie”=\>[],  
“Cache-Control”=\>“no-cache”}

Thanks & Regards  
Saurabh
