# Acts As Attachment :storage=\> :db\_system tutorial version

**URL:** <https://www.ruby-forum.com/t/acts-as-attachment-storage-db-system-tutorial-version/85336>\
**Category:** Rails\
**Created:** [February 12, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-storage-db-system-tutorial-version/85336 "2007-02-12T20:22:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@paron](https://www.ruby-forum.com/u/paron)\
**Post date:** [February 12, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-storage-db-system-tutorial-version/85336/1 "2007-02-12T20:22:12Z")

</div>

This is just a tweak to Rick O.'s tutorial, because his shows  
file\_system storage, and because I got file\_system running on the first  
go every time I tried, but only got db\_system running after a lot of  
struggling. I was never sure whether it was my db configuration or my  
acts\_as\_attachment configuration, so I kept fiddling one, then the  
other.

It took a while to realize SQL Server wasn’t EVER going to work. Once I  
switched to MySQL, it wasn’t bad at all. It doesn’t look like much now.

Here’s the migration as I used it:

##db/migrate/001\_create\_dvd\_covers.rb  
class CreateDvdCovers \< ActiveRecord::Migration  
def self.up  
create\_table :dvd\_covers do |t|  
t.column ‘dvd\_id’, :integer  
t.column “content\_type”, :string  
t.column “filename”, :string  
t.column “size”, :integer

```
  # used with thumbnails, always required
  t.column "parent_id", :integer
  t.column "thumbnail", :string

  # required for images only
  t.column "width", :integer
  t.column "height", :integer

  # required for db-based files only
  t.column "db_file_id", :integer
end

# only for db-based files
create_table :db_files, :force => true do |t|
  t.column :data, :binary, :limit=>3.megabytes
end

```

end

def self.down  
drop\_table :dvd\_covers

```
# only for db-based files
drop_table :db_files

```

end  
end

And the /app files:

##app/models/dvd\_cover.rb  
class DvdCover \< ActiveRecord::Base  
acts\_as\_attachment  
validates\_as\_attachment  
belongs\_to :db\_file  
end

##app/models/db\_file.rb  
class DbFile \< ActiveRecord::Base  
has\_one :dvd\_cover  
end

## app/views/dvd\_covers/index.rhtml

# DVD Covers

\<% @dvd\_covers.each do |dvd\_cover| -%\> - \<%= link\_to dvd\_cover.filename, :action =\> 'show', :id =\> dvd\_cover %\>
\<% end -%\>

\<%= link\_to 'New', :action =\> 'new' %\>

## app/views/dvd\_covers/new.rhtml

# New DVD Cover
\<% form\_for :dvd\_cover, :url =\> { :action =\> 'create' }, :html =\> { :multipart =\> true } do |f| -%\> 

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

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

\<% end -%\>

##(NOTE: app/views/dvd\_covers/show.rhtml isn’t needed for this version,  
since it’s a “send\_data” in the controller.)

## app/controllers/dvd\_covers\_controller.rb

class DvdCoversController \< ApplicationController  
def index  
@dvd\_covers = DvdCover.find(:all)  
end

def new  
@dvd\_cover = DvdCover.new  
end

def show  
@dvd\_cover = DvdCover.find(params[:id])  
send\_data @dvd\_cover.db\_file.data, :filename =\> @dvd\_cover.filename,  
:type =\> @dvd\_cover.content\_type, :disposition =\> ‘inline’  
end

def create  
@dvd\_cover = DvdCover.create! params[:dvd\_cover]  
redirect\_to :action =\> ‘show’, :id =\> @dvd\_cover  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘new’  
end  
end

Now, if your database is configured properly, that should work. MySQL  
only needed one tweak: on Windows, I went into C:\Program  
Files\MySQL\MySQL Server 5.0\my.ini and added a line to the [mysqld]  
section – max\_allowed\_packet=32M. Restarted MySQL and it just worked.

I never got SQL Server to work at all, so if you know the secret, post  
it for me, please.

Ron

---

<div class="post-metadata">

**Author:** ![paron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@paron](https://www.ruby-forum.com/u/paron)\
**Post date:** [February 1, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-storage-db-system-tutorial-version/85336/2 "2008-02-01T23:56:50Z")

</div>

Thanks for the post Ron. I finally got ActsAsAttachment working on my  
local machine with db storage.

Ben

On Feb 12 2007, 11:22 am, Ron P. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![paron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@paron](https://www.ruby-forum.com/u/paron)\
**Post date:** [August 4, 2008, 8:57am UTC](https://www.ruby-forum.com/t/acts-as-attachment-storage-db-system-tutorial-version/85336/3 "2008-08-04T08:57:40Z")

</div>

Hi,

I am trying to upload an image file, in MySql. When I try to upload it’s  
not throwing any error. But in the DB, it is always getting saved as  
"Ã¿Ã˜Ã¿Ã " in the Text. It is not getting saved as image.

When I tried to read the image and send(using send\_data method), it  
shows “No preview available”.

I tried with both acts\_as\_attachment and attachment\_fu. Any guess what  
the mistake I have made?

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![paron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecb155/32.png) [@paron](https://www.ruby-forum.com/u/paron)\
**Post date:** [August 4, 2008, 2:49pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-storage-db-system-tutorial-version/85336/4 "2008-08-04T14:49:04Z")

</div>

I have no experience with attachment\_fu or acts\_as\_attachment but I  
setup image uploads in an app yesterday using paperclip. The whole  
process took about 10 minutes but I already had imagemagick installed  
so that saved me some time.

Chris B

> **[羞草研究所隐藏入口 - 羞含草研究研所2021 - 羞羞app正式版下载 - 羞羞动漫破解版无限金币](http://www.randomutterings.com)**
>
> 羞草研究所隐藏入口,羞含草研究研所2021,羞羞app正式版下载,羞羞动漫破解版无限金币是一款最潮的手机短视频APP，提供ios苹果下载/安卓下载。片源丰富，高潮迭起！
