# Using :db\_file as storage type in attachment\_fu

**URL:** <https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019>\
**Category:** Rails\
**Created:** [January 14, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019 "2008-01-14T18:54:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thehunfromoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@thehunfromoz](https://www.ruby-forum.com/u/thehunfromoz)\
**Post date:** [January 14, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019/1 "2008-01-14T18:54:53Z")

</div>

I’ve successfully been using attachment\_fu with the :file\_system  
storage type, but am looking to switch over to using MySQL to store  
the images instead. First off, the reason I’m thinking of doing this  
is to hold all images centrally. Thinking being that it will help  
scalability, by avoiding filesystem related issues with multiple  
machines. Yeah, maybe I’m looking too far ahead ?.

Howandever, there’s a one liner in the attachment\_fu README that says

“When serving files from database storage, doing more than simply  
downloading the file is beyond the scope of this document.”

OK, I respect that, but … I’m also now lost. Problem being that the  
handy public\_filename() method only exists for file and S3 storage  
types. How does one take the binary data directly from the DB and  
display it in a view ?. I spent a little time peeking at the  
file\_column plugin, thinking it probably had code to do this by  
default, but … it didn’t jump right out at me … I’m more a jack of  
all trades rather than a master of RoR :0((.

This is surely easy enough ? … i.e taking image data from a database  
field ( a BLOB field in MySQL) and displaying it in an RHTML view.

Would very much appreciate a pointer on how its done, or a link to  
some sample code that does this.

Thanks in Advance,  
Attila

---

<div class="post-metadata">

**Author:** ![thehunfromoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@thehunfromoz](https://www.ruby-forum.com/u/thehunfromoz)\
**Post date:** [January 14, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019/2 "2008-01-14T21:32:48Z")

</div>

On Jan 14, 2008 9:53 AM, thehunfromoz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> "When serving files from database storage, doing more than simply  
> This is surely easy enough ? … i.e taking image data from a database  
> field ( a BLOB field in MySQL) and displaying it in an RHTML view.
> 
> Would very much appreciate a pointer on how its done, or a link to  
> some sample code that does this.

You don’t just store blobs in HTML, you’ll need to write some custom  
controller actions that read from the database and stream the blob  
data out. Look at the send\_data method in ActionController.

Problem is that Mongrel buffers everything from rails, so this will  
cause memory leaks if you’re serving large files.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![thehunfromoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@thehunfromoz](https://www.ruby-forum.com/u/thehunfromoz)\
**Post date:** [January 15, 2008, 12:54am UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019/3 "2008-01-15T00:54:04Z")

</div>

Hi Rick,

Thanks for both the plugin and the comment. I very much appreciate  
all the many plugin contributors to the land of RoR. I’ve been  
teaching myself Ruby and Rails for the last 9 months, and would not  
have been able to get anywhere near as productive without help from  
you folks.

As for my problem … your hint about memory leak issues brings some  
caution to my thinking, and am now thinking I’m better off for the  
time being deferring the problem until I actually need to solve it …  
re: if and when I need to scale to multiple machines ;0).

I will have a dabble though, just to see what might be involved.

Thanks again,  
Attila

---

<div class="post-metadata">

**Author:** ![thehunfromoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@thehunfromoz](https://www.ruby-forum.com/u/thehunfromoz)\
**Post date:** [January 15, 2008, 1:34am UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019/4 "2008-01-15T01:34:24Z")

</div>

This won’t help your scalability. MySQL doesn’t handle blob fields  
very well, and having to perform database queries to show assets is  
going to slow things down over time. There are considerably faster and  
more scalable methods for delivering assets from the filesystem. If  
you want to centralize the location of the files, you have many  
options, such as a separate assets server, NFS, SAN, or Amazon S3. Of  
course, I wouldn’t even bother with this until you really need it,  
because it’s not hard to switch (move the files to a shared partition,  
or sftp them over, then change a few attachment\_fu settings and URLs)

---

<div class="post-metadata">

**Author:** ![thehunfromoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@thehunfromoz](https://www.ruby-forum.com/u/thehunfromoz)\
**Post date:** [January 15, 2008, 12:44pm UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019/5 "2008-01-15T12:44:22Z")

</div>

Hi Jeff,

OK … that’s the sorta info I was looking for … i.e. whether MySQL  
was a good choice or not for images. I know a fair amount about the  
legacy centralised file storage options, I’ve a Telco on Sun Solaris  
background, but I’ve seen quite a few references to Amazons S3. I’ve  
not really looked into it, but I will.

Thanks,  
Attila

---

<div class="post-metadata">

**Author:** ![thehunfromoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@thehunfromoz](https://www.ruby-forum.com/u/thehunfromoz)\
**Post date:** [March 11, 2008, 12:39am UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019/6 "2008-03-11T00:39:58Z")

</div>

mariek wrote:

> Did any of You find attachment\_fu working with rails 2 version?

I have it working just fine, except it won’t save attachments to the  
databank. But after reading this thread, I guess I don’t care so much.  
But I’m sure someone somewhere does.

---

<div class="post-metadata">

**Author:** ![thehunfromoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/278dde/32.png) [@thehunfromoz](https://www.ruby-forum.com/u/thehunfromoz)\
**Post date:** [January 22, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/using-db-file-as-storage-type-in-attachment-fu/126019/7 "2008-01-22T18:59:51Z")

</div>

Did any of You find attachment\_fu working with rails 2 version?
